[英]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'.
我需要从lightboxes_tbl中选择所有灯箱,其中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.
例如,'aircraft-types'灯箱(id = 100 / lightbox_id = 100)将返回2个资产。 The 'maintenance' lightbox (id = 101 / lightbox_id = 101) would return 1 asset.
'维护'灯箱(id = 101 / lightbox_id = 101)将返回1个资产。
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()
利用
LEFT JOIN
和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 这是一个SQLFiddle演示
Recommended reading: 推荐阅读:
Join to the assts table: 加入assts表:
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. 那里有一个小技巧:
sum()
用于计算asst表中有多少行不为空,这对于没有资产的灯箱会产生总计为零的数字 - 当count()
不会做的时候使用左连接。
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. 顺便说一句,在mysql中,如果为true,则布尔结果为1,如果为false则为0,因此将条件求和整齐地计算它的真实次数。
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' 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 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_ida.id,a.lightbox_name,b.id,b.lightbox_id,b.asset_name,b.asset_id
from lightboxes_tbl a来自lightboxes_tbl a
join lightbox_assets_tbl b加入lightbox_assets_tbl b
on (a.id = b.lightbox_id)on(a.id = b.lightbox_id)
where a.author = "scott@co.com"其中a.author =“scott@co.com”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.