简体   繁体   English

MySQL:使用一个查询从第二个表返回行计数

[英]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 JOINCOUNT()

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_id a.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.

相关问题 如何从一个表中返回一行,其中包含MySQL中第二个表中匹配id的数量? - How do I return a row from one table with the number of matching id's from a second table in MySQL? MYSQL通过比较值使用一个查询的行数来限制一秒钟的结果 - MYSQL Use Row Count From One Query To Limit Results in A Second By Comparing Values MySQL-从同一表的2行返回一行,用第二个“ override”的填充字段覆盖第一个“ default”的内容 - MySQL - return one row from 2 rows in the same table, overwrite the contents of the first 'default' with the populated fields of the second 'override' mysql-将整个行从一个表复制到另一个表,并在一个查询中添加带条件的count(*) - mysql - copy entire row to from one table to another and add count(*) with conditions in one query 使用来自一个mysql的结果进行第二次查询 - using results from one mysql for a second query MySQL从第二个表计数 - Mysql count from a second table MYSQL查询计数表行 - MYSQL query to count table row 从一个MySQL表中选择,即使count为0,也返回count(id) - Selecting from one MySQL table, return count(id) even if count is 0 mysql从联接查询中的一个表中获取特定的唯一行数 - mysql get specific unique row count from one table in a join query MySQL查询,从一个表中查找并插入到第二个表中 - MySQL query, finding from one table and insert into second table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM