来自MySQL中多个表的COUNT(*)

如何从MySQL的多个表中选择COUNT(*)?

如:

SELECT COUNT(*) AS table1Count FROM table1 WHERE someCondition

JOIN??

SELECT COUNT(*) AS table2Count FROM table2 WHERE someCondition

CROSS JOIN? subqueries?

SELECT COUNT(*) AS table3Count FROM table3 WHERE someCondition

目标是返回此:

+-------------+-------------+-------------+

| table1Count | table2Count | table3Count |

+-------------+-------------+-------------+

| 14 | 27 | 0 |

+-------------+-------------+-------------+

回答:

您可以通过使用子查询来实现,每个tableCount一个子查询:

SELECT

(SELECT COUNT(*) FROM table1 WHERE someCondition) as table1Count,

(SELECT COUNT(*) FROM table2 WHERE someCondition) as table2Count,

(SELECT COUNT(*) FROM table3 WHERE someCondition) as table3Count

以上是 来自MySQL中多个表的COUNT(*) 的全部内容, 来源链接: utcz.com/qa/398900.html

回到顶部