MySQL使用GROUP_CONCAT和多个JOINS

时间:2015-02-05 14:49:35

标签: mysql join left-join group-concat

我有以下四个表格。我的查询正常工作,除了我需要让字段'AUTHORIZED_VIEWER'和'AUTHORIZED_VIEWER_EMAIL'返回所有值而不仅仅是第一个值。我相信这可以通过使用GROUP_CONCAT来完成,但是,我不确定应该如何实现这部分。 注意 - 尝试使用GROUP_CONCAT时,我必须使用以下语法,因为它返回了一个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             |
 +-------+-------------+-----------------+-------------------+--------------------------+--------------+

2 个答案:

答案 0 :(得分:1)

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

一个有趣的问题永远不会感谢分享和希望我们帮助过!

  1. 我们将group_concat添加到avt.authorized_vieweru.email
  2. 我们将distinct添加到group_concat,仅按要求撤回唯一值。
  3. 我们为每个非聚合值添加了group by
  4. 我们修改了where子句以拉入Scott作为评论者的所有灯箱。通过使用作者字段作为限制,我们排除了其他评论者。通过将过滤器基于灯箱的ID,我们保留所有用户;允许group_concat按需工作。
  5. 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 希望这能为你解决问题! (从基础主题中删除了几条评论,因为我现在已将它们包括在内或这里获得的信息。)

答案 1 :(得分:0)

试试这个: -

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
相关问题