我尝试了两种方法但在mysql中失败了。
/*see top 50% students, but this sql can't work*/
select * from student_table order by chinese_score desc limit count(*) * 0.5 ;
/*also can't work*/
set @num= floor((select count(*) from test.student_score)*0.5);
select * from student_table order by chinese_score desc limit @num ;
如何在mysql中解决?
答案 0 :(得分:1)
在 Mysql 中,可以使用user defined variables在单个查询中完成此操作。
您可以在一个语句中将值存储在用户定义的变量中 稍后在另一个声明中提及它。这使您可以通过 从一个陈述到另一个陈述的价值。
SELECT * FROM (
SELECT student_table.*, @counter := @counter +1 AS counter
FROM (SELECT @counter:=0) AS initvar, student_table
ORDER BY student_table.chinese_score DESC
) AS result
WHERE counter < (@counter/2) ORDER BY chinese_score DESC;