4

我有一张这样的座位号和标记表:

seat    marks
61      45
62      25
63      45
64      23
65      25
66      9
67      23

最高分是 100。现在我想显示有多少候选人在 10 秒、20 秒、30 秒、.... 100 秒内获得分数

marks     candidates_count
10        1
20        4
30        0
..        ..

等等。现在我知道了

SELECT seat, marks, count(marks) as counts from <table> group by marks order by counts  desc;

或者每 10 秒、20 秒和 30 秒执行一次

SELECT seat, marks from <table> where marks>10 and marks<=20 group by marks;

并获取我的 php 中返回的行数并返回结果,但这不是很优雅。必须有一种方法可以直接在 MySQL 中执行此操作,而无需使用 MySQL for 循环。

4

3 回答 3

2

尝试这个:

select (marks/10)*10 as marks,
       count(*) as candidates_count
from <table>
group by (marks/10)*10
于 2012-10-12T10:36:22.757 回答
2

以下肯定会为您工作

select (FLOOR(`marks`/ 10)+1)*10 as marks,
       count(*) as candidates_count
from <table>
group by (FLOOR(`marks`/ 10)+1)*10;
于 2012-10-12T10:53:16.197 回答
0

Try:

select ((marks/10)+1)*10, count(1) 
from tab
group by ((marks/10)+1)*10

But if you want to see 0 you have to prepare expected marks:

create table tab2
(  
  marks int
)

insert into tab2 values(10)
insert into tab2 values(20)
insert into tab2 values(30)
insert into tab2 values(40)
insert into tab2 values(50)
insert into tab2 values(60)
....
insert into tab2 values(100)

and use right join:

select t2.marks, count(tab.marks) 
from tab
right join tab2 t2 on t2.marks = ((tab.marks/10)+1)*10
group by t2.marks
order by 1
于 2012-10-12T10:56:45.927 回答