从两个类似的查询/表

时间:2018-01-31 16:22:16

标签: mysql database

我有两个或更多表,想要一起查询每个表的数量。这两个表具有相同的结构。

这是我的查询,以获取每个的计数:

SELECT count(*) FROM pt_test_orders 
WHERE payment_log = '100 - Success' AND DATE(created_at) = CURDATE()

SELECT count(*) FROM bs_test_orders 
WHERE payment_log = '100 - Success' AND DATE(created_at) = CURDATE()

如果可能的话,我希望得到这样的结果:

| table_name          | count |
-------------------------------
|     pt_test_orders  |     3 |
|     bs_test_orders  |     8 |

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

使用UNION

SELECT 'pt_test_orders' as TableName, count(*) FROM pt_test_orders 
WHERE payment_log = '100 - Success' AND DATE(created_at) = CURDATE()
UNION  
SELECT 'bs_test_orders'  as TableName, count(*) FROM bs_test_orders 
WHERE payment_log = '100 - Success' AND DATE(created_at) = CURDATE()