我有以下四個表。我的查詢正常工作的異常,我需要有字段'AUTHORIZED_VIEWER'和'AUTHORIZED_VIEWER_EMAIL'返回所有值不只是第一個。我相信這可以通過使用GROUP_CONCAT完成,但是,我不確定這部分應該如何實現。 注 - 嘗試使用GROUP_CONCAT的時候,我不得不使用下面的語法,因爲它是返回一個BLOB:MySQL使用GROUP_CONCAT與多JOINS
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小提琴結果:
+-------+----------------+--------------+-------------------+--------------------------+--------------+
|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 |
+-------+-------------+-----------------+-------------------+--------------------------+--------------+
@xQbert這非常接近。對於「[email protected]」不是作者,而是authorized_viewer的行,它僅列出[email protected]。如果可能,我需要列出所有authorized_viewers,其中[email protected]就是其中之一。那有意義嗎?謝謝! – azsl1326 2015-02-05 15:30:09
是的,我認爲你的第一次嘗試更接近。所以我期望的是,任何'[email protected]'是作者的燈箱都會返回,包括所有authorized_viewers(以及所有其他列出的信息 - 看起來是正確的)。此外,任何'[email protected]'爲authorized_viewer的燈箱都將被退回,其中包括其他authorized_viewers。用戶可以是作者或authorized_viewer,但不能同時使用。希望這可以幫助。感謝您的持續幫助。 – azsl1326 2015-02-05 17:20:43
我正在看這個小提琴,http://sqlfiddle.com/#!2/6f3d7/6/0,它列出了兩個資產磅#30 - 我不知道爲什麼。無論如何,我只是看着你更新的小提琴,我相信這是正確的!謝謝你的幫助。如果你想創建一個答案,我會標記它是正確的。 – azsl1326 2015-02-05 19:42:02