我正在寻找学生谁有最小的分数,这将是以下查询的结果。但是,我被要求不使用MIN()来编写查询。花了几个小时,但我找不到另一种解决办法。
select s.sname
from student s
where s.score =
(select min(s2.score)
from score s2)发布于 2014-11-06 03:13:48
这是一种方法,即使两名学生的分数相同,这种方法也能起作用。
SELECT distinct s1.sname
FROM student s1
LEFT JOIN student s2
ON s2.score < s1.score
WHERE s2.score IS NULL以下是使用极限的方法,该方法将返回分数最低的学生,但如果他们中的多人有相同的分数,则只有其中一人。
select sname
from student
order by score asc
limit 1发布于 2014-11-06 03:30:36
下面是JOIN方法的一个可能的替代方案:
select sname from student where score in
(select score from student order by score asc limit 1)发布于 2014-11-06 04:03:30
create table student (name varchar(10), score int);
insert into student (name, score) values('joe', 30);
insert into student (name, score) values('jim', 88);
insert into student (name, score) values('jack', 22);
insert into student (name, score) values('jimbo', 15);
insert into student (name, score) values('jo bob',15);
/* folks with lowest score */
select name, score from student where not exists(select 1 from student s where s.score < student.score);
/* the actual lowest score */
select distinct score from student
where not exists(select 1 from student s where s.score < student.score);请注意,不存在可能是残酷的低效,但它将在一小部分完成工作。
https://stackoverflow.com/questions/26771001
复制相似问题