MySQL 对多个 JOINS 使用 GROUP_CONCAT

MySQL Use GROUP_CONCAT with Multiple JOINS

我有以下四个表。我的查询工作正常,但我需要字段 'AUTHORIZED_VIEWER' 和 'AUTHORIZED_VIEWER_EMAIL' return 所有值而不仅仅是第一个值。我相信这可以通过使用 GROUP_CONCAT 来完成,但是,我不确定这部分应该如何实现。 注意 - 在尝试使用 GROUP_CONCAT 时,我不得不使用以下语法,因为它是 return 一个 BLOB:

CONVERT(GROUP_CONCAT(authorized_viewer) USING utf8)

这是四个表格:

users_tbl
+-----+------------------+
|id   |email             |  
+-----+------------------+
|10   | scott@co.com     |
|8    | cesar@co.com     |
|11   | kevin@co.com     |
|12   | jake@co.com      |
+-----+------------------+

authorized_viewers_tbl (authorized_viewer linked to id in users_tbl)
+-----+------------+------------------+
|id   |lightbox_id |authorized_viewer |   
+-----+------------+------------------+
|1    | 50         |11                |
|7    | 50         |8                 |
|3    | 31         |11                |
|5    | 30         |8                 |
|6    | 30         |11                |
|8    | 16         |11                |
|9    | 16         |10                |
|10   | 5          |10                |
|11   | 5          |11                |
+-----+------------+------------------+

lightboxes_tbl
+-----+------------------+---------------+
|id   |lightbox_name     |author         |   
+-----+------------------+---------------+
|5    | Test Lightbox #1 |jake@co.com    |
|16   | Test Lightbox #2 |cesar@co.com   |
|30   | Test Lightbox #3 |scott@co.com   |
|31   | Test Lightbox #4 |kevin@co.com   |
|50   | Test Lightbox #5 |cesar@co.com   |
+-----+------------------+---------------+

lightbox_assets_tbl
+-------+-------------+------------------+------------------=---+----------+
|id     |lightbox_id  |asset_name        |asset_path            | asset_id |
+-------+-------------+------------------+----------------------+----------+
|232    |30           |b757.jpg          |SWFs/b757.jpg         | 3810     |
|230    |31           |b757.jpg          |SWFs/b757.jpg         | 3810     |
|233    |16           |a321_takeoff.jpg  |SWFs/a321_takeoff.jpg | 3809     |
|234    |31           |a321_takeoff.jpg  |SWFs/a321_takeoff.jpg | 3809     |
|235    |50           |a330_landing.png  |SWFs/a330_landing.png | 3789     |
+-------+-------------+------------------+-----------------------+---------+

这是我目前正在使用的查询:

SELECT lb.id,
   lb.lightbox_name,
   lb.author,
   avt.authorized_viewer,
   u.email AS authorized_viewer_email,
   COUNT(lba.lightbox_id) total_assets
FROM lightboxes_tbl lb
LEFT JOIN lightbox_assets_tbl lba ON lb.id = lba.lightbox_id
LEFT JOIN authorized_viewers_tbl avt ON avt.lightbox_id = lb.id
LEFT JOIN users_tbl u ON u.id = avt.authorized_viewer
WHERE lb.author = 'scott@co.com'
  OR avt.authorized_viewer =
    (SELECT id
     FROM users_tbl
     WHERE email = 'scott@co.com')
GROUP BY lb.id
ORDER BY lb.lightbox_name ASC

SQL Fiddle

谢谢!

[编辑] 预期结果基于 SQL Fiddle:

 +-------+----------------+--------------+-------------------+--------------------------+--------------+
 |id     |lightbox_name   |author        |authorized_viewer  | email                    | total_assets |
 +-------+----------------+--------------+-------------------+--------------------------+--------------+
 |5      |Test Lightbox#1 |jake@co.com   |10,11              |scott@co.com,kevin@co.com |0             |             
 |16     |Test Lightbox#2 |cesar@co.com  |10,11              |scott@co.com,kevin@co.com |1             |
 |30     |Test Lightbox#3 |scott@co.com  |11,8               |kevin@co.com,cesar@co.com |1             |
 +-------+-------------+-----------------+-------------------+--------------------------+--------------+

试试这个:-

SELECT lb.id,
   lb.lightbox_name,
   lb.author,
   avt.authorized_viewer,
   u.email AS authorized_viewer_email,
   COUNT(lba.lightbox_id) total_assets
FROM lightboxes_tbl lb
LEFT JOIN lightbox_assets_tbl lba ON lb.id = lba.lightbox_id
LEFT JOIN authorized_viewers_tbl avt ON avt.lightbox_id = lb.id
LEFT JOIN users_tbl u ON u.id = avt.authorized_viewer
WHERE lb.author = 'scott@co.com'
OR avt.authorized_viewer =
(SELECT id
 FROM users_tbl
 WHERE email = 'scott@co.com')
GROUP BY lb.id, lb.lightbox_name, lb.author, avt.authorized_viewer, u.email
ORDER BY lb.lightbox_name

有一种更简洁的方法可以做到这一点,但我还没有时间考虑它。

一个有趣的问题,但仍然感谢您的分享,希望我们有所帮助!

  1. 我们将group_concat添加到avt.authorized_vieweru.email
  2. 我们将 distinct 添加到 group_concat 以仅根据要求提取唯一值。
  3. 我们为每个非聚合值添加了 group by
  4. 我们更改了 where 子句以拉入 Scott 担任审阅者的所有灯箱。通过使用作者字段作为限制,我们排除了其他评论者。通过将过滤器基于灯箱的 Id,我们保留了所有用户;这允许 group_concat 按需要工作。

.

SELECT lb.id,
       lb.lightbox_name,
       lb.author,
       group_concat(distinct avt.authorized_viewer) a,
       group_concat(distinct u.email) b,
       COUNT(distinct lba.id) total_assets
FROM lightboxes_tbl lb
LEFT JOIN lightbox_assets_tbl lba ON lb.id = lba.lightbox_id
LEFT JOIN authorized_viewers_tbl avt ON avt.lightbox_id = lb.id
LEFT JOIN users_tbl u ON u.id = avt.authorized_viewer
where lb.author = 'scott@co.com'
or 
lb.id in (Select lightbox_ID 
          from authorized_Viewers_tbl X
          INNER JOIN users_Tbl U on U.ID = X.authorized_Viewer
          WHERE email = 'scott@co.com')
GROUP BY lb.id, lb.lightbox_name, lb.author
ORDER BY lb.lightbox_name ASC

http://sqlfiddle.com/#!2/ccc6a/2/0 希望这能帮您解决问题! (从基本主题中删除了一些评论,因为我现在已经将它们或此处收集的信息包括在内。)