MySQL Use GROUP_CONCAT with Multiple JOINS

azsl1326

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

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 = '[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

Thanks!

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

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
  2. We add distinct to the group_concat to only pull back Unique values as requested.
  3. We added group by for each of the non-aggregated values.
  4. We altered the where clause to pull in all light boxes which Scott was a reviewer. 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; which allows 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 = '[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 Hope this wraps things up for you! (purged several comments from base topic as I've now included them or the information garnered here.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

group_concat with multiple joins in MySQL

From Dev

Mysql - optimisation - multiple group_concat & joins using having

From Dev

SQL Query, Group_Concat of multiple left joins and nested queries

From Dev

GROUP_CONCAT with FIND_IN_SET, multiple joins

From Dev

SQL Query, Group_Concat of multiple left joins and nested queries

From Dev

How to use sum and joins within group_concat

From Dev

How to use sum and joins within group_concat

From Dev

mysql multiple group_concat order preservation

From Dev

mysql GROUP_CONCAT DISTINCT multiple columns

From Dev

MySQL - is it possible to use group_concat in an IN() statement?

From Dev

Invalid use of group function (group_concat and MySQL)

From Dev

Understanding use of multiple SUMs with LEFT JOINS in mysql

From Dev

MySQL use GROUP_CONCAT when INSERTing data

From Dev

how to use select query in a Group_concat sub query in mysql

From Dev

MySQL Group_Concat Not In

From Dev

Group_concat use?

From Dev

Group_concat use?

From Dev

Slow MySQL query with multiple joins, max() and group by

From Dev

mysql - multiple joins

From Dev

mySQL multiple inner joins

From Dev

Multiple joins in MySQL table

From Dev

Optimize multiple JOINs in MySQL

From Dev

Multiple JOINS - Mysql

From Dev

Multiple Inner Joins - MySQL

From Dev

mySQL multiple inner joins

From Dev

MySQL joins multiple tables

From Dev

MySQL: update with join using GROUP_CONCAT: ERROR 1111 (HY000): Invalid use of group function

From Dev

If condition with group_concat in mysql

From Dev

Mysql with GROUP_CONCAT in subselect