简体   繁体   English

MySQL对多个JOINS使用GROUP_CONCAT

[英]MySQL Use GROUP_CONCAT with Multiple JOINS

I have the following four tables. 我有以下四个表。 My query is working correctly with the exception that I need to have the field 'AUTHORIZED_VIEWER' and 'AUTHORIZED_VIEWER_EMAIL' return all values not just the first one. 我的查询工作正常,但我需要使字段“ AUTHORIZED_VIEWER”和“ AUTHORIZED_VIEWER_EMAIL”返回所有值,而不仅仅是第一个值。 I believe that this can be done by using GROUP_CONCAT, however, I am not sure exactly how this part should be implemented. 我相信这可以通过使用GROUP_CONCAT来完成,但是,我不确定该如何实现。 Note - when attempting to use GROUP_CONCAT, I had to use the following syntax as it was return a BLOB: 注–尝试使用GROUP_CONCAT时,我必须使用以下语法,因为它返回了BLOB:

CONVERT(GROUP_CONCAT(authorized_viewer) USING utf8)

Here are the four tables: 这是四个表:

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     |
+-------+-------------+------------------+-----------------------+---------+

Here's the query that I am currently using: 这是我当前正在使用的查询:

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小提琴

Thanks! 谢谢!

[EDIT] Expected results based upon 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             |
 +-------+-------------+-----------------+-------------------+--------------------------+--------------+

There is a cleaner way of doing this but I haven't had the time to think it though yet. 有一种更干净的方法可以执行此操作,但是我还没有时间考虑它。

A fun question never the less thanks for sharing and hope we helped! 一个有趣的问题还是感谢您的分享,并希望我们能为您提供帮助!

  1. We add group_concat to avt.authorized_viewer and u.email 我们将group_concat添加到avt.authorized_vieweru.email
  2. We add distinct to the group_concat to only pull back Unique values as requested. 我们添加distinctgroup_concat的要求,只拉了回来唯一的值。
  3. We added group by for each of the non-aggregated values. 我们为每个非汇总值添加了group by
  4. We altered the where clause to pull in all light boxes which Scott was a reviewer. 我们更改了where子句,以拉入所有Scott曾担任审阅者的灯箱。 By using the author field as the limit we excluded the other reviewers. 通过使用作者字段作为限制,我们排除了其他审阅者。 By basing the filter on the Id of the lightbox, we keep all users; 通过将过滤条件基于灯箱ID,我们可以保留所有用户; which allows group_concat to work as desired. 这使得group_concat可以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 Hope this wraps things up for you! http://sqlfiddle.com/#!2/ccc6a/2/0希望以上内容对您有所帮助! (purged several comments from base topic as I've now included them or the information garnered here.) (从基本主题中清除了一些评论,因为我现在已将其包括在内或此处获得的信息。)

Try this:- 尝试这个:-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM