我有以下四个表。我的查询工作正常,但我需要使字段“ AUTHORIZED_VIEWER”和“ AUTHORIZED_VIEWER_EMAIL”返回所有值,而不仅仅是第一个。我相信这可以通过使用GROUP_CONCAT来完成,但是,我不确定该如何实现。注–尝试使用GROUP_CONCAT时,由于返回BLOB,因此必须使用以下语法:
CONVERT(GROUP_CONCAT(authorized_viewer) USING utf8)
这是四个表:
users_tbl
+-----+------------------+
|id |email |
+-----+------------------+
|10 | [email protected] |
|8 | [email protected] |
|11 | [email protected] |
|12 | [email protected] |
+-----+------------------+
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 |[email protected] |
|16 | Test Lightbox #2 |[email protected] |
|30 | Test Lightbox #3 |[email protected] |
|31 | Test Lightbox #4 |[email protected] |
|50 | Test Lightbox #5 |[email protected] |
+-----+------------------+---------------+
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 = '[email protected]'
OR avt.authorized_viewer =
(SELECT id
FROM users_tbl
WHERE email = '[email protected]')
GROUP BY lb.id
ORDER BY lb.lightbox_name ASC
谢谢!
[编辑]基于SQL Fiddle的预期结果:
+-------+----------------+--------------+-------------------+--------------------------+--------------+
|id |lightbox_name |author |authorized_viewer | email | total_assets |
+-------+----------------+--------------+-------------------+--------------------------+--------------+
|5 |Test Lightbox#1 |[email protected] |10,11 |[email protected],[email protected] |0 |
|16 |Test Lightbox#2 |[email protected] |10,11 |[email protected],[email protected] |1 |
|30 |Test Lightbox#3 |[email protected] |11,8 |[email protected],[email protected] |1 |
+-------+-------------+-----------------+-------------------+--------------------------+--------------+
有一种更干净的方法可以执行此操作,但是我还没有时间考虑它。
有趣的问题还是感谢您的分享,并希望我们有所帮助!
group_concat
到avt.authorized_viewer
和u.email
distinct
了group_concat
,仅根据要求拉回唯一值。group by
为每个非汇总值添加了。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 = '[email protected]'
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 = '[email protected]')
GROUP BY lb.id, lb.lightbox_name, lb.author
ORDER BY lb.lightbox_name ASC
http://sqlfiddle.com/#!2/ccc6a/2/0希望以上内容对您有所帮助!(从基本主题中清除了一些评论,因为我现在已将其包括在内或此处获得的信息。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句