Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL使用WHERE条件对多个表中的行进行计数_Mysql - Fatal编程技术网

MySQL使用WHERE条件对多个表中的行进行计数

MySQL使用WHERE条件对多个表中的行进行计数,mysql,Mysql,当我在下面单独运行查询时,会得到正确的结果 SELECT COUNT(hospital_id) FROM `hospital` WHERE org_id='1' // Result: 0 SELECT COUNT(pharmacy_id) FROM `pharmacy` WHERE org_id='1' // Result: 1 SELECT COUNT(fire_station_id) FROM `fire_station` WHERE org_id='1' // Result: 3 SELE

当我在下面单独运行查询时,会得到正确的结果

SELECT COUNT(hospital_id) FROM `hospital` WHERE org_id='1' // Result: 0
SELECT COUNT(pharmacy_id) FROM `pharmacy` WHERE org_id='1' // Result: 1
SELECT COUNT(fire_station_id) FROM `fire_station` WHERE org_id='1' // Result: 3
SELECT COUNT(als_id) FROM `als` WHERE org_id='1' // Result: 3
SELECT COUNT(units_id) FROM `units` WHERE org_id='1'; //Result: 3
但是我需要使用MySQL的任何概念来获得所有结果

我的尝试:

SELECT COUNT(hospital_id) AS Hospital FROM `hospital` WHERE org_id='1'
UNION
SELECT COUNT(pharmacy_id) AS Pharmacy FROM `pharmacy` WHERE org_id='1'
UNION
SELECT COUNT(fire_station_id) AS Station FROM `fire_station` WHERE org_id='1'
UNION
SELECT COUNT(als_id) AS Als FROM `als` WHERE org_id='1'
UNION
SELECT COUNT(units_id) AS Units FROM `units` WHERE org_id='1';
这是我当前的结果:

    +------------+
    |   Hospital |
    +------------+
    |          0 |
    |          1 |
    |          3 |
    +------------+
+------------+------------+------------+-------+--------+
|   Hospital |   Pharmacy |   Station  |  Als  |   Units|
+------------+------------+------------+-------+--------+
|          0 |          1 |         3  |     3 |      3 |
+------------+------------+------------+-------+--------+
这是我想要的结果:

    +------------+
    |   Hospital |
    +------------+
    |          0 |
    |          1 |
    |          3 |
    +------------+
+------------+------------+------------+-------+--------+
|   Hospital |   Pharmacy |   Station  |  Als  |   Units|
+------------+------------+------------+-------+--------+
|          0 |          1 |         3  |     3 |      3 |
+------------+------------+------------+-------+--------+
我还提到了堆栈问题 但是没有得到预期的结果。 请更新我的查询或建议MySQL的任何其他概念


谢谢大家

特定查询的问题是
UNION
。使用
UNION ALL
。事实上,除非您特别想承担删除重复项的开销,否则请始终使用
UNION ALL

在任何情况下,您的查询都很接近。将它们作为子查询放入
选择中:

SELECT (SELECT COUNT(hospital_id) AS Hospital FROM `hospital` WHERE org_id='1') as hospital,
       (SELECT COUNT(pharmacy_id) AS Pharmacy FROM `pharmacy` WHERE org_id='1') as pharmacy,
       (SELECT COUNT(fire_station_id) AS Station FROM `fire_station` WHERE org_id='1') as Station,
       (SELECT COUNT(als_id) AS Als FROM `als` WHERE org_id='1') as als,
       (SELECT COUNT(units_id) AS Units FROM `units` WHERE org_id='1') as units;
您还可以将子查询放在
FROM
子句中,并使用
交叉连接来组合它们

试试这个:

SELECT
    (SELECT COUNT(hospital_id) FROM hospital WHERE org_id='1') as Hospital,
    (SELECT COUNT(pharmacy_id) FROM pharmacy WHERE org_id='1') as Pharmacy,
    (SELECT COUNT(fire_station_id) FROM fire_station WHERE org_id='1') as Station,
    (SELECT COUNT(als_id) FROM als WHERE org_id='1') as Als,
    (SELECT COUNT(units_id) FROM units WHERE org_id='1') as Units;

这是一个很好的解释。谢谢真的很好。