You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SQL JOIN与子查询问题:查询无结果(No Rows Selected)求助

问题分析与解决

错误原因

  1. 子查询筛选逻辑错误:subject_name不可能同时等于两个不同的字符串,原代码中用AND连接两个相等条件会导致子查询返回空结果集,主查询自然无法匹配到数据。需将AND替换为OR,或用IN来匹配多个目标科目名称。
  2. 缺少排序语句:需求要求按科目名称降序排序,原代码未添加对应的ORDER BY子句。

修正后的代码

方式一:修正子查询逻辑

select s.subject_name, min(m.value) as MIN_MARK
from mark m
join subject s on m.subject_id = s.subject_id 
where m.subject_id in (select subject_id from subject
                       where subject_name = 'Software Engineering' 
                       OR subject_name='Computer Programming') 
group by s.subject_name
order by s.subject_name DESC;

方式二:简化查询(直接过滤科目名称)

无需嵌套子查询,直接在主查询中匹配目标科目名称,代码更简洁:

select s.subject_name, min(m.value) as MIN_MARK
from mark m
join subject s on m.subject_id = s.subject_id 
where s.subject_name IN ('Software Engineering', 'Computer Programming')
group by s.subject_name
order by s.subject_name DESC;

内容的提问来源于stack exchange,提问作者Levi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 07:25:18