I have a table and I need to get the number of rows using different criteria. Currently I use 3 queries one after another:
我有一个表,我需要使用不同的标准获取行数。目前我一个接一个地使用3个查询:
- SELECT COUNT(status) FROM projects WHERE project='1'
- SELECT COUNT(status) FROM projects WHERE project='1' AND status>'10'
- SELECT COUNT(status) FROM projects WHERE project='1' AND status>'20'
SELECT COUNT(status)FROM项目WHERE project ='1'
SELECT COUNT(status)FROM projects WHERE project ='1'AND status> '10'
SELECT COUNT(status)FROM projects WHERE project ='1'AND status> '20'
How do I merge these queries into a single query?
如何将这些查询合并到单个查询中?
P.S. There are 30 different statuses, so GROUP BY status isn't much of an option.
附:有30种不同的状态,因此GROUP BY状态不是一个选项。
1 个解决方案
#1
5
You can use UNION
like this:
您可以像这样使用UNION:
SELECT COUNT(status), ('1') as info FROM projects WHERE project='1'
UNION
SELECT COUNT(status), ('1-10') as info FROM projects WHERE project='1' AND status>'10'
UNION
SELECT COUNT(status), ('1-20') as info FROM projects WHERE project='1' AND status>'20'
Read more at:
阅读更多:
#1
5
You can use UNION
like this:
您可以像这样使用UNION:
SELECT COUNT(status), ('1') as info FROM projects WHERE project='1'
UNION
SELECT COUNT(status), ('1-10') as info FROM projects WHERE project='1' AND status>'10'
UNION
SELECT COUNT(status), ('1-20') as info FROM projects WHERE project='1' AND status>'20'
Read more at:
阅读更多: