当存在多个条件时,如何在mysql中统一三个不同的查询?

时间:2020-12-08 00:49:11

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个查询:

  1. SELECT COUNT(status) FROM projects WHERE project='1'
  2. SELECT COUNT(status)FROM项目WHERE project ='1'

  3. SELECT COUNT(status) FROM projects WHERE project='1' AND status>'10'
  4. SELECT COUNT(status)FROM projects WHERE project ='1'AND status> '10'

  5. SELECT COUNT(status) FROM projects WHERE project='1' AND status>'20'
  6. 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:

阅读更多:

https://dev.mysql.com/doc/refman/4.1/en/union.html

#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:

阅读更多:

https://dev.mysql.com/doc/refman/4.1/en/union.html