2015-02-05 196 views
1

我有以下四個表。我的查詢正常工作的異常,我需要有字段'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 Fiddle

謝謝!

[編輯] 預計基於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    | 
+-------+-------------+-----------------+-------------------+--------------------------+--------------+ 
+0

@xQbert這非常接近。對於「[email protected]」不是作者,而是authorized_viewer的行,它僅列出[email protected]。如果可能,我需要列出所有authorized_viewers,其中[email protected]就是其中之一。那有意義嗎?謝謝! – azsl1326 2015-02-05 15:30:09

+0

是的,我認爲你的第一次嘗試更接近。所以我期望的是,任何'[email protected]'是作者的燈箱都會返回,包括所有authorized_viewers(以及所有其他列出的信息 - 看起來是正確的)。此外,任何'[email protected]'爲authorized_viewer的燈箱都將被退回,其中包括其他authorized_viewers。用戶可以是作者或authorized_viewer,但不能同時使用。希望這可以幫助。感謝您的持續幫助。 – azsl1326 2015-02-05 17:20:43

+0

我正在看這個小提琴,http://sqlfiddle.com/#!2/6f3d7/6/0,它列出了兩個資產磅#30 - 我不知道爲什麼。無論如何,我只是看着你更新的小提琴,我相信這是正確的!謝謝你的幫助。如果你想創建一個答案,我會標記它是正確的。 – azsl1326 2015-02-05 19:42:02

回答

1

有這樣做的更清潔的方式,但我還沒有時間去儘管如此。

一個有趣的問題永遠不會多謝分享,並希望我們幫助!

  1. 我們添加group_concatavt.authorized_vieweru.email
  2. 我們添加distinctgroup_concat的要求,只拉了回來唯一的值。我們爲每個非彙總值添加了group by
  3. 我們修改了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 = '[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 希望這包東西給你! (清除了幾個來自基本主題的評論,因爲我現在已經包含了這些內容或獲得的信息。)

+0

我並不是靠近SQL專家的任何地方,所以如果有更清晰的方式來寫這個,你有時間去做,我很樂意看到它。再次感謝!!! – azsl1326 2015-02-05 22:09:23

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 = '[email protected]' 
OR avt.authorized_viewer = 
(SELECT id 
FROM users_tbl 
WHERE email = '[email protected]') 
GROUP BY lb.id, lb.lightbox_name, lb.author, avt.authorized_viewer, u.email 
ORDER BY lb.lightbox_name