mysql四个统计数量语句 怎么合并在一个select里
时间: 2024-01-22 16:17:38 浏览: 64
可以使用UNION ALL将四个统计数量语句合并在一个SELECT语句中,如下:
```
SELECT COUNT(*) as count1, NULL as count2, NULL as count3, NULL as count4 FROM table1
UNION ALL
SELECT NULL as count1, COUNT(*) as count2, NULL as count3, NULL as count4 FROM table2
UNION ALL
SELECT NULL as count1, NULL as count2, COUNT(*) as count3, NULL as count4 FROM table3
UNION ALL
SELECT NULL as count1, NULL as count2, NULL as count3, COUNT(*) as count4 FROM table4
```
上面的语句中,每个SELECT语句都返回一个统计数量,使用UNION ALL将这四个语句合并在一起。在每个SELECT语句中,使用NULL填充其他列,以确保每个SELECT语句返回相同的列数和列顺序。最终的结果将是一个包含四个统计数量的结果集。
阅读全文