oracle left join 无法查出左表的全部数据,很疑惑

hongjiang1011 2008-09-09 05:36:27
有如下sql语句
    select st.name,count(si.id)
from t_school_type st
left join t_school_info si on st.id = si.t_s_id
where si.record_status = 1
group by st.name;



t_school_type 有七条记录,
id name
4c58f3f5-e38d-4954-9d38-c97d9e0b3331 普通初中
4c58f3f6-e37d-4984-9d38-c27d3e0b3332 职业初中
4c58f3f5-e38d-4954-9458-c27d360b3333 九年一贯制学校
4c58f3f5-e38d-4954-9458-c27d360b3334 普通小学
4c58f3f5-e38d-4954-9458-c27d360b3335 特殊教育学校
4c58f3f5-e38d-4954-9458-c27d360b3336 完全中学
4c58f3f5-e38d-4954-9458-c27d360b3337 普通高中

t_school_info 中t_s_id 是外键

为什么查询以后的结果只有六条,左表中的数据不能去不查询出来呢,疑惑
...全文
2303 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
linxuanyuzhu 2008-09-11
  • 打赏
  • 举报
回复
select name,count(id) from (select * from t_school_type st left join t_school_info si on st.id = si.t_s_id) where record_status=1 group by name
xieyueqing 2008-09-10
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 hongjiang1011 的回复:]
能否解释一下,left join 应该是能够查出左表中的全部的数据的
[/Quote]
是你的理解有问题。

打个比方吧,Select * from Table 是能查询出所有行的。但是你要是 在后面加个 where 条件,他就只能查出满足条件的行了。这时候,你是否怀疑Select * from Table 不能查出所有的行呢?

你那个语句不能查出所有左边的数据,同样是因为有Where 条件在。
sleepzzzzz 2008-09-10
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 hongjiang1011 的回复:]
能否解释一下,left join 应该是能够查出左表中的全部的数据的
[/Quote]
你怎么怀疑起left join了?是你的SQL表达不正确,当然取不到你想要的结果。按6楼的写法你试试
hongjiang1011 2008-09-10
  • 打赏
  • 举报
回复
能否解释一下,left join 应该是能够查出左表中的全部的数据的
mzl_mzl 2008-09-10
  • 打赏
  • 举报
回复
where在from之后执行,原因就这么简单,这可是基本中的基本内容,无语。。。。
skystar99047 2008-09-10
  • 打赏
  • 举报
回复
select st.name,count(si.id)
from t_school_type st
left join t_school_info si on st.id = si.t_s_id
where si.record_status = 1
group by st.name;
------------------------------
the result will be
STEP 1:
select * from t_school_type st left join t_school_info si on st.id = si.t_s_id;
this sql will find out every entry in table t_school_type;
we can name the result as "resulttable"

but after you add where condition
STEP 2:
select name,count(id) from resulttable where record_status=1 group by name;
the result will be filted.

So that's why you got the result like that, it's true.
79cy 2008-09-10
  • 打赏
  • 举报
回复
因为你要用右表的WHERE做过滤,所以隐含了一层过滤就是右表要有值。
jadeyu 2008-09-09
  • 打赏
  • 举报
回复
不明白为什么呢?
susie8449 2008-09-09
  • 打赏
  • 举报
回复
左表中的数据不能去不查询出来呢,疑惑?????
sleepzzzzz 2008-09-09
  • 打赏
  • 举报
回复
select st.name,count(si.id)
from t_school_type st
left join (select * from t_school_info where record_status = 1)si on st.id = si.t_s_id
group by st.name;
sleepzzzzz 2008-09-09
  • 打赏
  • 举报
回复
先过滤掉si.record_status = 1,然后再left jion
lily1314 2008-09-09
  • 打赏
  • 举报
回复
select st.name,count(si.id)
from t_school_type st
left join t_school_info si on st.id = si.t_s_id
and si.record_status = 1
group by st.name;

把where换成and试试
kinlin 2008-09-09
  • 打赏
  • 举报
回复 1
select st.name,count(si.id)
from t_school_type st,t_school_info si
where st.id = si.t_s_id(+)
and si.record_status(+) = 1
group by st.name;
hongjiang1011 2008-09-09
  • 打赏
  • 举报
回复
不行的,要用where条件过滤右表中的数据
xuehong520 2008-09-09
  • 打赏
  • 举报
回复
select st.name,count(si.id)
from t_school_type st
left join t_school_info si on st.id = si.t_s_id
group by st.name;

把where 条件去掉。

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧