通过1个查询获得2个相关组?

时间:2013-11-20 11:50:14

标签: mysql sql database-design

我有以下情况:

  • 1项目表
  • 1会员表
  • 1页表

每个“项目”属于“成员”和“页面”

$query = mysql_query("SELECT i.id, i.title, mem.realname, p.pagename
                FROM items AS i
                LEFT JOIN members AS mem ON (mem.ID_MEMBER = i.author)
                LEFT JOIN pages AS p ON (p.id = i.page)
                WHERE i.id LIKE '$item_id'");

这提供了我需要的基本信息:

  • id:3,title:音乐视频,真实姓名:Peter Smith,pagename:Youtube

所以我想要的是获得两组“3相关项目”,其中:

  • id.author = Peter Smith
  • id.page = Youtube

因此输出将是这样的:

  • id:3,title:音乐视频,真实姓名:Peter Smith,pagename:Youtube

  • 来自Peter Smith的
  • 项目:第5项,第4项,第8项(随机显示)

  • 来自Youtube的
  • 项目:第1项,第2项,第9项(随机显示)

我是否需要执行2个额外查询才能获取此信息?或者我可以在所有查询中打包吗?

非常感谢!

更新

我添加了@valex

建议的Group_concat
   SELECT i.id, i.title, mem.realname, p.pagename,
          ( select CONCAT('Items from ', mem.realname,': ',
                (select GROUP_CONCAT(Title) 
                        FROM
                           (Select Title from items where author=
                                    (select author from items 
                                              WHERE id = '$item_id' LIMIT 1)
                             ORDER BY RAND() LIMIT 3) T)              
                )
           ) as items_from_author,
          ( select CONCAT('Items from ',p.pagename,': ',
                (select GROUP_CONCAT(Title) 
                        FROM
                           (Select Title from items where page=
                                        (select page from items 
                                              WHERE id = '$item_id' LIMIT 1)

                             ORDER BY RAND() LIMIT 3) T)              
                )
           ) as items_from_page


            FROM items AS i
            LEFT JOIN members AS mem ON (mem.ID_MEMBER = i.author)
            LEFT JOIN pages AS p ON (p.id = i.page)
            WHERE i.id LIKE '$item_id'

但是得到以下错误:

Invalid query: Unknown column 'i.author' in 'where clause'

有什么想法吗?

更新2

目前使用@valex建议的“4级”查询。有没有办法让它适用于2个级别?

SELECT i.id, i.title, mem.realname, p.pagename,
          ( select CONCAT('Items from ', mem.realname,': ',
                (select GROUP_CONCAT(Title) 
                        FROM
                           (Select Title from items where author=
                                    (select author from items 
                                              WHERE id = '$item_id' LIMIT 1)
                             ORDER BY RAND() LIMIT 3) T)              
                )
           ) as items_from_author,
          ( select CONCAT('Items from ',p.pagename,': ',
                (select GROUP_CONCAT(Title) 
                        FROM
                           (Select Title from items where page=
                                        (select page from items 
                                              WHERE id = '$item_id' LIMIT 1)

                             ORDER BY RAND() LIMIT 3) T)              
                )
           ) as items_from_page


            FROM items AS i
            LEFT JOIN members AS mem ON (mem.ID_MEMBER = i.author)
            LEFT JOIN pages AS p ON (p.id = i.page)
            WHERE i.id LIKE '$item_id'

似乎这样可以以某种方式进行优化。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用GROUP_CONCAT()函数将这些列表作为当前行中的字段。

例如:

SELECT i.id, i.title, mem.realname, p.pagename,
              ( select CONCAT('Items from ', mem.realname,': ',
                    (select GROUP_CONCAT(Title) 
                            FROM
                               (Select Title from items where author=i.author
                                 ORDER BY RAND() LIMIT 3) T)              
                    )
               ) as items_from_author,
              ( select CONCAT('Items from ',p.pagename,': ',
                    (select GROUP_CONCAT(Title) 
                            FROM
                               (Select Title from items where page=i.page
                                 ORDER BY RAND() LIMIT 3) T)              
                    )
               ) as items_from_page


                FROM items AS i
                LEFT JOIN members AS mem ON (mem.ID_MEMBER = i.author)
                LEFT JOIN pages AS p ON (p.id = i.page)
                WHERE i.id LIKE '$item_id'

更新:似乎MySQL无法通过两个级别访问外部查询字段。因此,请尝试使用以下内容:

SELECT i.id, i.title, mem.realname, p.pagename,
              ( select CONCAT('Items from ', mem.realname,': ',
                    (select GROUP_CONCAT(Title) 
                            FROM
                               (Select Title from items where author=
                                        (select author from items 
                                                  WHERE id = '$item_id' LIMIT 1)
                                 ORDER BY RAND() LIMIT 3) T)              
                    )
               ) as items_from_author,
              ( select CONCAT('Items from ',p.pagename,': ',
                    (select GROUP_CONCAT(Title) 
                            FROM
                               (Select Title from items where page=
                                            (select page from items 
                                                  WHERE id = '$item_id' LIMIT 1)

                                 ORDER BY RAND() LIMIT 3) T)              
                    )
               ) as items_from_page


                FROM items AS i
                LEFT JOIN members AS mem ON (mem.ID_MEMBER = i.author)
                LEFT JOIN pages AS p ON (p.id = i.page)
                WHERE i.id LIKE '$item_id'