如何查询组2条件?

时间:2021-10-27 15:56:37

I have table here.

我在这里有表。

如何查询组2条件?

How can i

我怎么能

SELECT 
    sum(gload1) as 'g1',
    sum(gload2) as 'g2'.
    sum(gload3) as 'g3',
    sum(gload4) as 'g4' 
FROM member 
WHERE age = 15-20,21-25,26-30 
GROUP BY gender;

result like this : Just sample

像这样的结果:只是样本

如何查询组2条件?

How can i query like this. Thank for advamce.

我怎么能这样查询呢?感谢advamce。

3 个解决方案

#1


1  

Try like this:

试试这样:

SELECT
    CASE 
        WHEN age BETWEEN 15 AND 20 THEN '15-20'
        WHEN age BETWEEN 20 AND 30 THEN '20-30'
        ELSE '30-...'
    END as age_group,
    gender,
    sum(gload1) as gload1_total
FROM 
    member
GROUP BY 
    CASE 
        WHEN age BETWEEN 15 AND 20 THEN '15-20'
        WHEN age BETWEEN 20 AND 30 THEN '20-30'
        ELSE '30-...'
    END,
    gender

if it doesn't work, you might have to do a subquery: inner query will add the range string to every row and outer query will group by it.

如果它不起作用,您可能需要执行子查询:内部查询将向每一行添加范围字符串,外部查询将由它进行分组。

#2


2  

SELECT 
  CASE 
    WHEN `age`>=15 AND `age`<=20 THEN '15-20'
    WHEN `age`>=21 AND `age`<=25 THEN '21-25'
    WHEN `age`>=26 AND `age`<=30 THEN '26-30'
  END AS `AgeRange`,
  SUM(`gload1`) AS `g1`, 
  SUM(`gload2`) AS `g2`, 
  SUM(`gload3`) AS `g3`, 
  SUM(`gload4`) AS `g4`
FROM 
  member 
GROUP BY `AgeRange`

See SQL Fiddle

看到SQL小提琴

#3


1  

You need to create fake table representing the rows. Try this:

您需要创建表示行的伪表。试试这个:

SELECT AgeBrackets.Age as Age, sum(gload1) as gload1, sum(gload2) as gload2, sum(gload4) AS gload
FROM 
(SELECT '15-20' AS Age, 15 as stdt, 20 as eddt
UNION
SELECT '21-25', 21, 25
UNION
SELECT '26-30', 26, 30) AS AgeBrackets
JOIN member
ON member.age BETWEEN AgeBrackets.stdt AND AgeBrackets.eddt
GROUP BY Age
ORDER BY Age;

#1


1  

Try like this:

试试这样:

SELECT
    CASE 
        WHEN age BETWEEN 15 AND 20 THEN '15-20'
        WHEN age BETWEEN 20 AND 30 THEN '20-30'
        ELSE '30-...'
    END as age_group,
    gender,
    sum(gload1) as gload1_total
FROM 
    member
GROUP BY 
    CASE 
        WHEN age BETWEEN 15 AND 20 THEN '15-20'
        WHEN age BETWEEN 20 AND 30 THEN '20-30'
        ELSE '30-...'
    END,
    gender

if it doesn't work, you might have to do a subquery: inner query will add the range string to every row and outer query will group by it.

如果它不起作用,您可能需要执行子查询:内部查询将向每一行添加范围字符串,外部查询将由它进行分组。

#2


2  

SELECT 
  CASE 
    WHEN `age`>=15 AND `age`<=20 THEN '15-20'
    WHEN `age`>=21 AND `age`<=25 THEN '21-25'
    WHEN `age`>=26 AND `age`<=30 THEN '26-30'
  END AS `AgeRange`,
  SUM(`gload1`) AS `g1`, 
  SUM(`gload2`) AS `g2`, 
  SUM(`gload3`) AS `g3`, 
  SUM(`gload4`) AS `g4`
FROM 
  member 
GROUP BY `AgeRange`

See SQL Fiddle

看到SQL小提琴

#3


1  

You need to create fake table representing the rows. Try this:

您需要创建表示行的伪表。试试这个:

SELECT AgeBrackets.Age as Age, sum(gload1) as gload1, sum(gload2) as gload2, sum(gload4) AS gload
FROM 
(SELECT '15-20' AS Age, 15 as stdt, 20 as eddt
UNION
SELECT '21-25', 21, 25
UNION
SELECT '26-30', 26, 30) AS AgeBrackets
JOIN member
ON member.age BETWEEN AgeBrackets.stdt AND AgeBrackets.eddt
GROUP BY Age
ORDER BY Age;