0

我正在寻找一些查询帮助

这是下表数据

Name    Runs    Status
Ram         50  out
Ram         103 not out
Krish   51  out
Sam         15  out
Ram         15  out
Krish   78  not out

我期待一个查询给出以下结果

Name    Total   >100    >50&<100    TotalTimes  Notout 
Ram        168  1           1         3          1
Sam        15   0           0         1          0
Krish      129  0           2         2          1

我可以在 Group By 功能的帮助下编写查询来获取总数,Totaltimes,其余的我都坚持

这是我提出的查询

select Name, sum(Runs) as total, count(*) as totalTimes
from tempTable 
where classID IN (Select classID from upcoming_Clases where classes_id=175) 
group by Name order by total desc 

我正在使用 Mysql 数据库

4

2 回答 2

3

您可以使用SUM()withIF()来测试您的标准:

SELECT
    Name,
    SUM(Runs) AS Total,
    SUM(IF(Runs>100, 1, 0)) AS `>100`,
    SUM(IF(Runs>50 AND Runs<100), 1, 0) AS `>50&<100`,
    COUNT(*) AS TotalTimes,
    SUM(IF(Status='not out', 1, 0)) AS Notout
FROM tempTable
WHERE classID IN (SELECT classID FROM upcoming_Clases WHERE classes_id = 175)
GROUP BY Name
ORDER BY Total DESC
于 2012-05-01T10:11:20.397 回答
3

您可以使用案例执行此操作:

select Name, 
       sum(Runs) as total, 
       count(case when Runs>100 then 1 end) `>100`,
       count(case when Runs>50 and Runs<100 then 1 end) `>50&<100`,
       count(*) as totalTimes,
       count(case when Status='not out' then 1 end) `Not Out`
from tempTable 
where classID IN (Select classID from upcoming_Clases where classes_id=175) 
group by Name order by total desc
于 2012-05-01T10:11:32.067 回答