如何从MySQL中的三个表中获取数据?

时间:2014-04-02 06:55:17

标签: php mysql sql join

我有三张桌子:

  1. gallery_items
  2. gallery_album_items
  3. gallery_tags
  4. 我设法加入前两个表但是如何将gallery_tags表添加到以下查询中?:

    SELECT *
    FROM gallery_items
    RIGHT JOIN gallery_album_items 
    ON gallery_items.id=gallery_album_items.id 
    WHERE gallery_items.name LIKE '%{$term}%' 
    OR gallery_items.description LIKE '%{$term}%'
    

    基本上我要添加的内容:

    OR gallery_tags.tag LIKE '%{$term}%'
    

    表格结构

    gallery_items
    id   name     description
    12   Sharon   This is sharon's description
    26   Rhett    This is rhett's description
    33   Lincoln  This is lincoln's description
    
    
    gallery_album_items
    id   album
    12   14 
    26   12
    33   7
    
    
    gallery_tags
    id   item     tag
    23   12       blonde
    43   26       tall
    3    33       glasses
    

    基本上我想要搜索"金发"的结果: Sharon 金发,位于相册中。

    (你不必提供一个echo语句。这只是向你展示结果的一个例子。)

    编辑 - 最终答案

    SELECT DISTINCT gallery_items.id 
    FROM gallery_items
    RIGHT OUTER JOIN gallery_album_items ON gallery_items.id = gallery_album_items.id
    LEFT JOIN gallery_tags ON gallery_items.id = gallery_tags.item
    WHERE gallery_items.active !=  '0'
    AND (gallery_items.name LIKE '%blonde%'
    OR gallery_items.description LIKE '%blonde%'
    OR gallery_tags.tag LIKE '%blonde%'
    

5 个答案:

答案 0 :(得分:2)

SELECT *

FROM gallery_items 

RIGHT OUTER JOIN gallery_album_items 

ON gallery_items.id=gallery_album_items.id 

LEFT OUTER JOIN  gallery_tags 

ON gallery_tags.id   =  gallery_album_items.id     

WHERE gallery_items.name LIKE '%{$term}%' 

OR gallery_items.description LIKE '%{$term}%'

OR gallery_tags.tag LIKE '%{$term}%'

答案 1 :(得分:1)

关于这个

SELECT *
FROM gallery_items
RIGHT JOIN gallery_album_items 
   ON gallery_items.id=gallery_album_items.id 
LEFT JOIN gallery_tags
   ON gallery_tags.id = gallery_items.id
WHERE gallery_items.name LIKE '%{$term}%' 
OR gallery_items.description LIKE '%{$term}%'

答案 2 :(得分:1)

SELECT *
FROM gallery_items
RIGHT JOIN gallery_album_items 
ON gallery_items.id=gallery_album_items.id
LEFT JOIN gallery_tags on gallery_items.id on gallery_tags.id  
WHERE gallery_items.name LIKE '%{$term}%' 
OR gallery_items.description LIKE '%{$term}%'

答案 3 :(得分:1)

请尝试这个

select gi.name, gai.ablum, gt.tag 
    from 
    gallery_items as gi JOIN gallery_album_items as gai 
    on gt.id=gai.id 
    Join gallery_tags as gt 
    on gi.id=gt.items

答案 4 :(得分:0)

您可以使用别名来编写连接查询,以便在考虑逻辑时更好地理解,而不是使用表名。 我尝试了一个,希望它会有所帮助。


创建表格g_i( id int, name varchar(20), descp nvarchar(max) )

插入g_i值(12,' asf 12','说明12') 插入g_i值(26,' asf 26','描述26') 插入g_i值(33,' asf 33','描述33') 从g_i

中选择*

创建表格g_a( id int, 专辑varchar(20) ) 插入g_a值(12,'专辑12') 插入g_a值(26,'专辑26') 插入g_a值(33,'专辑33') 从g_a

中选择*

create table g_t( id int, item int, 标签nvarchar(40) ) 插入g_t值(23,12,'标记12') 插入g_t值(43,46,'标记26') 插入g_t值(3,33,'标记33') 从g_t

中选择*

SELECT *

FROM g_i as i RIGHT JOIN g_a as ON i.id = a.id. LEFT JOIN g_t as t on t.item = i.id

在哪里i.name LIKE'%t%'或者i.descp LIKE'%t%'