简体   繁体   English

MySQL-Count具有WHERE条件的多个表中的行

[英]MySQL-Count rows in more than one table with WHERE condition

When I'm run below the query separately getting the correct result. 当我在查询下方运行时,分别得到正确的结果。

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

But I need all the result in single using any concept of MySQL. 但我需要使用MySQL的任何概念单独使用所有结果。

My try: 我的尝试:

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';

This is my current result: 这是我目前的结果:

    +------------+
    |   Hospital |
    +------------+
    |          0 |
    |          1 |
    |          3 |
    +------------+

This is my desired result: 这是我想要的结果:

+------------+------------+------------+-------+--------+
|   Hospital |   Pharmacy |   Station  |  Als  |   Units|
+------------+------------+------------+-------+--------+
|          0 |          1 |         3  |     3 |      3 |
+------------+------------+------------+-------+--------+

I'm also refer the Stack Question Count rows in more than one table with tSQL But not getting the desired result. 我也使用tSQL在多个表中引用Stack Question Count行但是没有得到所需的结果。 Please update my query or suggest any other concept of MySQL. 请更新我的查询或建议MySQL的任何其他概念。

Thanks To ALL! 谢谢大家!

The problem with your specific query is UNION . 您的特定查询的问题是UNION Use UNION ALL . 使用UNION ALL In fact, always use UNION ALL , unless you specifically want to incur the overhead of removing duplicates. 实际上,除非您特别想要消除重复项的开销,否则请始终使用UNION ALL

In any case, your queries are close. 无论如何,您的查询都很接近。 Put them as subqueries in the SELECT : 将它们作为子查询放在SELECT

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;

You can also put the subqueries in the FROM clause and use CROSS JOIN to combine them. 您还可以将子查询放在FROM子句中,并使用CROSS JOIN来组合它们。

Try this: 尝试这个:

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;

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

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