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

如何将现有表与self-join查询结果表进行JOIN关联以得到正确结果?

代码存在的问题

  1. 最终查询的返回字段仅包含music_band表的name和genretitel,没有把关联子查询中的discription字段加入SELECT列表,所以结果不会显示你需要的分类描述内容。
  2. 如果你预期仅保留两边完全匹配的记录,当前用的LEFT JOIN会保留所有乐队记录,哪怕没有对应的分类描述,会出现多余的空值行。

修改方案

如果需要保留全部乐队记录,哪怕没有对应分类描述,调整如下:

select band.name, band.genretitel, disc.discription
from music_band as band
left join (
select g.discription, m.title 
from genre g 
join genre m on g.title = m.main_title) disc
on band.genretitel = disc.title

如果仅需要保留有对应分类描述的乐队记录,把LEFT JOIN替换为INNER JOIN即可:

select band.name, band.genretitel, disc.discription
from music_band as band
inner join (
select g.discription, m.title 
from genre g 
join genre m on g.title = m.main_title) disc
on band.genretitel = disc.title

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 02:27:03