简体   繁体   English

如何使用一个查询对多个表中的行进行计数

[英]How to count rows from multiple tables using one query

My DB contains information about specific projects. 我的数据库包含有关特定项目的信息。 Each project is made of multiple parts, and each part has one or more photo galleries. 每个项目都由多个部分组成,每个部分都有一个或多个照相馆。 I want to know if I can count the number of project parts, galleries and photos associated with any given project using only one query. 我想知道是否可以仅使用一个查询来计算与任何给定项目关联的项目部分,画廊和照片的数量。 Currently there is 1 project in the system that has 3 parts, 5 galleries and 302 photos. 当前,系统中有1个项目,包含3个部分,5个画廊和302张照片。

This query (with thanks to many here from SO) works fine to count the number of parts per project, which currently is 3: 此查询(感谢SO的许多帮助)可以很好地计算每个项目的零件数,目前为3:

// count number of parts per project
$conn = dbConnect('query');
$galNumb = "SELECT COUNT(*) FROM pj_parts WHERE pj_id = ?";
$stmt = $conn->prepare($galNumb);
$stmt->bind_param('i', $project);
$stmt->bind_result($galTotal);
$stmt->execute();
$stmt->store_result();
$stmt->fetch();

Can this be changed to count not only the number of parts (pj_part) but also count the number of galleries and photos from their respective tables? 可以更改为不仅计算零件数(pj_part),而且还计算其各自表中的画廊和照片的数量吗? I'm trying to streamline my queries and learn more about making my code efficient. 我正在尝试简化查询,并了解有关使代码高效的更多信息。 Any help would be sincerely appreciated. 任何帮助将由衷的感谢。

UPDATE: 更新:

I'm not certain how to do the table structure formatting here, but here it is in plain text: 我不确定这里如何进行表结构格式化,但是这里是纯文本格式:

pj_parts
pj_part_id (primary key)
pj_id (project number)
pj_part (part number within project)

pj_part_galleries
pj_gallery_id (primary key)
pj_id (project number)
pj_part (part number within project)
pj_gallery (gallery number)

pj_photos
pj_photo_id (primary key)
pj_id (project number)
pj_part (part number within project)
pj_gallery_number

I hope this helps, if not, please let me know what to do to reformat it. 我希望这会有所帮助,如果没有帮助,请让我知道如何重新格式化。

If you use UNION 如果您使用UNION

SELECT 'pj_parts' as tablename, COUNT(*) as c 
    FROM pj_parts WHERE something=something
UNION 
SELECT 'pj_galleries' as tablename, COUNT(*) as c 
    FROM some_other_table WHERE something=something
UNION 
SELECT 'pj_photos' as tablename, COUNT(*) as c 
    FROM some_other_other_table WHERE something=something

you'll get this 你会得到这个

tablename         |   c
------------------+--------
pj_parts          |  3
pj_galleries      |  5
pj_photos         |  302

which you can iterate through to perhaps populate an array or whatever (question doesn't specify) 您可以迭代以填充数组或其他内容(问题未指定)

How about something like: 怎么样:

SELECT 
COUNT(DISTINCT p.pj_part_id) AS parts,
COUNT(DISTINCT g.pj_gallery_id) AS galleries,
COUNT(DISTINCT ph.pj_photo_id) AS photos,
 FROM 
pj_parts p
LEFT JOIN pj_part_galleries g ON (g.pj_part = p.pj_part_id)
LEFT JOIN pj_part_photos ph ON (ph.pj_part = p.pj_part_id)
WHERE p.pj_id = ?

You'll have to add in the correct table/column names. 您必须添加正确的表/列名称。

Edit: If using mysqli you can do the following to bind: Replace: 编辑:如果使用mysqli,则可以进行以下绑定:替换:

$stmt->bind_result($galTotal);

With: 附:

$stmt->bind_result($partsTotal,$galleriesCount,$photosCount);

NB: Here I've changed $galTotal to $partsTotal. 注意:在这里,我将$ galTotal更改为$ partsTotal。

Edit2: Updated to use column/table names. Edit2:更新为使用列/表名称。

Guessing on your structure, but something like this? 猜测您的结构,但是这样的事情?

SELECT 
    COUNT(DISTINCT p.part_id) parts ,
    COUNT(DISTINCT g.gal_id) galleries ,
    COUNT(DISTINCT ps.pic_id) photos
FROM pj_parts p
    LEFT JOIN {galleries} g on p.part_id = g.part_id
    LEFT JOIN {photos} ps on g.gal_id = ps.gal_id
WHERE p.pj_id = ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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