如何用'where'加入2个表

时间:2014-08-14 12:45:45

标签: mysql sql

我解释了我的问题。 我有这张桌子:

      artist                          artist_pic
-------------------       ---------------------------------------
| id | name | age |       | id_artist | picUrl | picDescription |
-------------------       ---------------------------------------
   |                          /\
   ----------------------------

每位艺术家都有很多照片。

这是我的查询(可行)

SELECT a.*, ai.* 
     FROM artist a INNER JOIN artist_pic ai on ai.id_artist = a.id  
         where ai.id_artist = '" + idArtist + "' and ai.elemento = 'artist'

现在我应该“添加”另一个名为artist_video的表

      artist                          artist_video
-------------------       ---------------------------------------
| id | name | age |       | id_artist | video| videoDescription |
-------------------       ---------------------------------------
   |                          /\
   ----------------------------

我试过这样的事,但我没有得到正确的记录

SELECT a.*, ai.*, av.* 
   FROM artist a 
       INNER JOIN artist_pic ai on ai.id_artist = a.id  
       INNER JOIN artist_video av on av.id_artist = a.id
         where ai.id_artist = 2 and ai.elemento = 'artist' and av.id_artist = 
          2 and av.elemento = 'artist'

例如,我应该得到的是:

michael jackson   Img/5345.jpg     concert      video/5345.avi     concert
michael jackson   Img/3453.jpg     family       video/2344.avi     family

但现在我得到的是(很多次同样的记录)

michael jackson   Img/5345.jpg     concert      video/5345.avi     concert
michael jackson   Img/3453.jpg     family       video/2344.avi     family
michael jackson   Img/5345.jpg     concert      video/5345.avi     concert
michael jackson   Img/3453.jpg     family       video/2344.avi     family
michael jackson   Img/5345.jpg     concert      video/5345.avi     concert
michael jackson   Img/3453.jpg     family       video/2344.avi     family
michael jackson   Img/5345.jpg     concert      video/5345.avi     concert
michael jackson   Img/3453.jpg     family       video/2344.avi     family
michael jackson   Img/5345.jpg     concert      video/5345.avi     concert
michael jackson   Img/3453.jpg     family       video/2344.avi     family

3 个答案:

答案 0 :(得分:0)

我猜你想要所有的原始唱片,还有那些有视频的唱片。如果是这样,这可能会解决您的问题:

SELECT a.*, ai.*, av.* 
FROM artist a INNER JOIN
     artist_pic ai
     on ai.id_artist = a.id and ai.elemento = 'artist' LEFT JOIN
     artist_video av
     on av.id_artist = a.id and av.elemento = 'artist'
where a.id_artist = 2;

答案 1 :(得分:0)

您可以将WHERE条件添加到此查询中,并参阅

SELECT  a.*, ai.*, av.* 
FROM    artist a 
JOIN    artist_pic ai on ai.id_artist = a.id  
JOIN    artist_video av on av.id_artist = a.id
WHERE   a.id = 2 

答案 2 :(得分:0)

您必须使用UNION命令合并两个查询:

SELECT a.*, 0 as is_video, ai.primarykey
     FROM artist a INNER JOIN artist_pic ai on ai.id_artist = a.id  
         where ai.id_artist = '" + idArtist + "' and ai.elemento = 'artist'
UNION
SELECT a.*, 1 as is_video, av.primarykey
     FROM artist a INNER JOIN artist_video av on av.id_artist = a.id  
         where av.id_artist = '" + idArtist + "' and av.elemento = 'artist'

请注意,它只适用于两个部分具有相同列类型的情况,因此您可能无法使用ai。*和av。*,而是分别指定每个列。我还添加了一个“is_video”列来区分图片和视频(如果需要)。

如果您为artist_picture和artist_video添加名称和类型列,则应该为您提供以下内容:

   michael jackson   Img/5345.jpg     concert
   michael jackson   Img/3453.jpg     family
   michael jackson   video/5345.avi     concert
   michael jackson   video/2344.avi     family