简体   繁体   中英

MySQL: Return row count from second table using one query

I have the following two tables. I need to select all the lightboxes from lightboxes_tbl where author ='scott@co.com'. That's obviously the easy part. Where I am stuck is that I also want to select in the same query the number of assets in each lightbox. For example, 'aircraft-types' lightbox (id = 100 / lightbox_id = 100) would return 2 assets. The 'maintenance' lightbox (id = 101 / lightbox_id = 101) would return 1 asset.

Thanks!

lightboxes_tbl
+-----+----------------+---------------+---------------------+
|id   |lightbox_name   |author         |authoried_viewers    |
+-----+----------------+---------------+---------------------+
|100  | aircraft-types |scott@co.com   |jon@co.com,aj@co.com |
+-----+----------------+---------------+---------------------+
|101  | maintenance    |scott@co.com   |nicole@co.com        |
+-----+----------------+---------------+---------------------+


lightbox_assets_tbl
+-----+-------------+-------------+---------------+----------+
|id   |lightbox_id  |asset_name   |asset_path     | asset_id |
+-----+-------------+-------------+---------------+----------+
|1    |100          |a321.jpg     |project1/imgs/ | 3700     |
+-----+-------------+-------------+---------------+----------+
|2    |100          |b757.jpg     |project1/imgs/ | 3444     |
+-----+-------------+-------------+---------------+----------+
|3    |101          |engine.jpg   |project4/imgs/ | 1444     |
+-----+-------------+-------------+---------------+----------+

Make use of LEFT JOIN and COUNT()

SELECT l.*, COUNT(a.lightbox_id) total_assets
  FROM lightboxes_tbl l LEFT JOIN lightbox_assets_tbl a
    ON l.id = a.lightbox_id
 WHERE l.author = 'scott@co.com'
 GROUP BY l.id

Output:

|  ID |  LIGHTBOX_NAME |       AUTHOR |    AUTHORIED_VIEWERS | TOTAL_ASSETS |
|-----|----------------|--------------|----------------------|--------------|
| 100 | aircraft-types | scott@co.com | jon@co.com,aj@co.com |            2 |
| 101 |    maintenance | scott@co.com |        nicole@co.com |            1 |

Here is a SQLFiddle demo

Recommended reading:

Join to the assts table:

select
  lb.id, lb.lightbox_name, lb.author, lb.authoried_viewers,
  sum(a.id is not null) asset_count
from lightboxes_tbl
left join lightbox_asset_tbl a
  on a.lightbox_id = lb.id
where author ='scott@co.com'
group by lb.id, lb.lightbox_name, lb.author, lb.authoried_viewers

There's a little trick in there: sum() is used to count how many rows are not null in the asst table, which will produce a total of zero for light boxes that have no assets - something that count() won't do when using a left join.

BTW, in mysql a boolean result is 1 if true, 0 if false, so summing a condition neatly counts how many times it was true.

You probably want something along the lines of this query...

SELECT LBT.id, LBT.lightbox_name, LBT.author, LBTA.id, LBTA.asset_name, LBTA.asset_path, LBTA.asset_id FROM lightboxes_tbl LBT JOIN lightbox_assets_tbl LBTA ON LBTA.lightbox_id = LBT.id WHERE author = 'scott@co.com'

azsl1326

this I write simple query for example, hope it helps,

select
a.id, a.lightbox_name, b.id, b.lightbox_id, b.asset_name, b.asset_id
from lightboxes_tbl a
join lightbox_assets_tbl b
on (a.id = b.lightbox_id)
where a.author = "scott@co.com"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM