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. I believe that this can be done by using GROUP_CONCAT, however, I am not sure exactly how this part should be implemented. Note - when attempting to use GROUP_CONCAT, I had to use the following syntax as it was return a 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
Thanks!
[EDIT] Expected results based upon 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!
group_concat
to avt.authorized_viewer
and u.email
distinct
to the group_concat
to only pull back Unique values as requested.group by
for each of the non-aggregated values.group_concat
to work as desired..
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! (purged several comments from base topic as I've now included them or the information garnered here.)