要求查询出学号大于谷雨(谷雨的学号为s25)年龄小于谷雨的学生姓名
于是用了
select sname
from s
where sno >
(select sno
from s
where sname ='谷雨')
and age <
(select age
from s
where sname ='谷雨')
但最终结果有学号为s3到s9的学生姓名
要求查询出学号大于谷雨(谷雨的学号为s25)年龄小于谷雨的学生姓名
于是用了
select sname
from s
where sno >
(select sno
from s
where sname ='谷雨')
and age <
(select age
from s
where sname ='谷雨')
但最终结果有学号为s3到s9的学生姓名
关注【以下回答由 GPT 生成】
可以使用子查询的方式将关闭结果与外部查询的结果进行比较,以解决该问题。
以下是优化后的查询语句:
select sname
from s
where sno > (
select sno
from s
where sname = '谷雨'
)
and age < (
select age
from s
where sno = (
select sno
from s
where sname = '谷雨'
)
);
通过将子查询的条件从sname改为sno,确保内层子查询仅返回指定学生的学号。这样,外部查询将使用该学号与其他学生的学号进行比较,并确保返回结果符合要求。
请注意,由于没有提供数据表结构和示例数据,因此无法进行进一步的测试和验证。以上代码仅是一种优化方案,但无法保证没有其他错误。
【相关推荐】