有多个where子句的select查询在一个结果表中返回多个结果行吗?

时间:2013-10-30 10:12:09

标签: php mysql sql performance query-optimization

我有一个查询,例如:

SELECT COUNT(id) AS user_list FROM tbl_users
WHERE (enrolled = 1 AND age >=15 AND age < 20)
OR (enrolled = 1 AND age >=20 AND age < 25)
OR (enrolled = 1 AND age >=25 AND age < 30)
OR (enrolled = 1 AND age >=30)

如何让它为一行和一个表中的每个条件返回结果:

user_list
---------
   18    --(enrolled = 1 AND age >=15 AND age < 20)
   12    --(enrolled = 1 AND age >=20 AND age < 25)
   22    --(enrolled = 1 AND age >=25 AND age < 30) 
   56    --(enrolled = 1 AND age >=30)

6 个答案:

答案 0 :(得分:2)

正如已经提到的,你可以使用一个案例并在一个查询中执行所有操作,但是你不需要使用子查询,虽然我不是它的忠实粉丝,MySQL允许你使用列别名group by,所以以下内容将起作用:

SELECT  CASE WHEN (enrolled = 1 AND age >=15 AND age < 20) THEN '15-19'
            WHEN (enrolled = 1 AND age >=20 AND age < 25) THEN '20-24'
            WHEN (enrolled = 1 AND age >=25 AND age < 30) THEN '25-29'
            WHEN (enrolled = 1 AND age >=30) THEN '30+'
            ELSE 'Other'
        END AS AgeRange 
        COUNT(id) AS user_list 
FROM    tbl_users
WHERE   Enrolled = 1
AND     Age >= 15
GROUP BY AgeRange;

注意,我还添加了一个where子句,以避免扫描冗余数据。

<强> Example on SQL Fiddle

但是,如果您丢失了数据,将返回一个空表,而不是:

AGERANGE    USER_LIST
15-19       0
20-24       0
25-29       0
30+         0

正如您所料。为了解决这个问题,我将创建一个伪表来进行选择,然后将你的数据加入:

SELECT  t.Name AS Age_Range,
        COUNT(u.ID) AS User_List
FROM    (   SELECT  '15-19' AS Name, 15 AS LowerBound, 20 AS UpperBound, 1 AS Enrolled
            UNION ALL
            SELECT  '20-24' AS Name, 20 AS LowerBound, 25 AS UpperBound, 1 AS Enrolled
            UNION ALL
            SELECT  '25-29' AS Name, 25 AS LowerBound, 30 AS UpperBound, 1 AS Enrolled
            UNION ALL
            SELECT  '30+' AS Name, 30 AS LowerBound, 9999999 AS UpperBound, 1 AS Enrolled
        ) t
        LEFT JOIN tbl_Users u
            ON u.Enrolled = t.enrolled
            AND u.Age >= t.LowerBound
            AND u.Age < t.UpperBound
GROUP BY t.Name;

<强> Example on SQL Fiddle

答案 1 :(得分:1)

试试这个方法

create table t (list1 int,list2 int,list3 int,list4 int)

insert into t
SELECT 
SUM(case when age >=15 AND age < 20 then 1 else 0 end) AS user_list1,
SUM(case when age >=20 AND age < 25 then 1 else 0 end) AS user_list2,
SUM(case when age >=25 AND age < 30 then 1 else 0 end) AS user_list3,
SUM(case when age >=15 AND age < 20 then 1 else 0 end) AS user_list4
FROM tbl_users
WHERE enrolled = 1 ;

select list1 from t union all
select list2 from t union all
select list3 from t union all
select list4 from t 

答案 2 :(得分:0)

有以下SQL子句可以提供帮助:

Group byCaseSubqueries ...查看它们并使用它,应该很有趣,自己弄清楚...

你可以考虑在你的查询中有另一列(例如15到18之间的情况,然后是1,case ...然后2,......)作为Range,识别一个范围并按此分组...

这是一个如何将范围标识为子查询中的列,并在子查询中按范围分组的查询的示例:

select count(*) as Count, r as Range 
    from 
        (select 
        (case 
        when enrolled = 1 AND age >=20 AND age < 25 then 'r1' 
        when enrolled = 1 AND age >=25 AND age < 30 then 'r2' 
        end) as r 
        from Text)          
    as groups 
group by r

这导致以下结果:

范围|计数

r1 | 10

r2 | 7

其中r1可以是您的范围,对应于“已登记= 1年龄&gt; = 20年龄&lt; 25”左右,在案例子句中定义。

答案 3 :(得分:0)

你可以使用工会:

SELECT COUNT(id) AS user_list
  FROM tbl_users
 WHERE (enrolled = 1 AND age >= 15 AND age < 20)
union
SELECT COUNT(id)
  FROM tbl_users
 where (enrolled = 1 AND age >= 20 AND age < 25)
union
SELECT COUNT(id)
  FROM tbl_users
 where (enrolled = 1 AND age >= 25 AND age < 30)
union
SELECT COUNT(id) FROM tbl_users where (enrolled = 1 AND age >= 30)

答案 4 :(得分:0)

使用4个简单查询而不是一个复杂的

,性能应该没有问题

答案 5 :(得分:0)

这很简单

select count(1),ceil(age/5)*5 from tbl_users where enrolled=1 group by ceil(age/5)