再问:SQL查询问题:如何让不存在的记录也显示?

FoxLinn 2007-04-05 09:44:15
我昨天提问,表达不清楚(http://community.csdn.net/Expert/topic/5442/5442197.xml?temp=.6215479),现在重新开帖


表a:
id cname
1 张
2 林
3 王
4 刘

表b:
id charge note
1 30 晚班
3 80 加班

如何实现如下结果(按条件加班查询):
id cname charge
1 张 0或null或干脆显示空白
2 林 0或null或干脆显示空白
3 王 80
4 刘 0或null或干脆显示空白


我的sql:
Select A.id,A.cname,IsNull(B.charge, 0) As charge
From A
Left Outer Join B
On A.id = B.id
where b.note='加班'

查询结果:

3 王 80 加班

其他没有了

...全文
447 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
xhsd78901234 2007-04-05
  • 打赏
  • 举报
回复
加and b.note='加班'没有任何意义呀!
paoluo 2007-04-05
  • 打赏
  • 举报
回复
用關聯應該比用子查詢的效率高些。
gahade 2007-04-05
  • 打赏
  • 举报
回复
如果B表中有多条,我写的那种就会出错.
但如果又不想汇总sum(charge)的话,就用鱼老大那种写法.

向老大学习@!:)
FoxLinn 2007-04-05
  • 打赏
  • 举报
回复
谢谢 wangdehao(找找找(现在很幸福))

sql 我是自学的,有很多地方不明白
FoxLinn 2007-04-05
  • 打赏
  • 举报
回复
嗯,同意楼上的想法!

如果有几十万数据,效率那个好?
wangdehao 2007-04-05
  • 打赏
  • 举报
回复
把where改成and就行了,楼主关键需要弄明白on跟where条件的不同
Select A.id,A.cname,IsNull(B.charge, 0) As charge
From A
Left Outer Join B
On A.id = B.id
and b.note='加班'
---on是连接条件,where是连接后的过滤条件
hertcloud 2007-04-05
  • 打赏
  • 举报
回复
select a.id,a.cname,(select charge from b where a.id=b.id and note='加班') as charge
from a

select a.id,a.cname,b.charge from a
left outer join b
on a.id=b.id and b.note='加班'

这个两种 效率谁好呢?
leo_lesley 2007-04-05
  • 打赏
  • 举报
回复
create table a(id int,cname varchar(10))
insert a select 1,'张' union all
select 2, '林' union all
select 3, '王' union all
select 4, '刘'

create table b(id int, charge int, note varchar(10))
insert b
select 1, 30 , '晚班' union all
select 3, 80 , '加班'

Select A.id,A.cname,IsNull(B.charge, 0) As charge
From A , B where b.note='加班' and A.id *= B.id
honkerhero 2007-04-05
  • 打赏
  • 举报
回复
Select A.id,A.cname,IsNull(B.charge, 0) As charge
From A
Left Outer Join B
On A.id = B.id

把WHERE 去掉
FoxLinn 2007-04-05
  • 打赏
  • 举报
回复
to:gahade
按照你的,出现如下错误:

消息 512,级别 16,状态 1,第 7 行
子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
hertcloud 2007-04-05
  • 打赏
  • 举报
回复
select a.id,a.cname,(select charge from b where a.id=b.id and note='加班') as charge
from a
这个学了
paoluo 2007-04-05
  • 打赏
  • 举报
回复
gahade(与君共勉),這個問題用子查詢不如直接用關聯好吧。:)
gahade 2007-04-05
  • 打赏
  • 举报
回复
drop table a,b
go
create table a(id int,cname varchar(20))
insert into a
select 1,'张'
union select 2,'林'
union select 3,'王'
union select 4,'刘'

create table b(id int,charge int,note varchar(20))
insert into b
select 1,30,'晚班'
union all select 3,80,'加班'


select a.id,a.cname,(select charge from b where a.id=b.id and note='加班') as charge
from a

/*
id cname charge
----------- -------------------- -----------
1 张 NULL
2 林 NULL
3 王 80
4 刘 NULL

(所影响的行数为 4 行)
*/
FoxLinn 2007-04-05
  • 打赏
  • 举报
回复
谢谢 paoluo(一天到晚游泳的鱼) !
gahade 2007-04-05
  • 打赏
  • 举报
回复
select a.id,a.cname,(select charge from b where a.id=b.id and note='加班')
from a
paoluo 2007-04-05
  • 打赏
  • 举报
回复
你將Where 改為And就可以了
paoluo 2007-04-05
  • 打赏
  • 举报
回复
不建議使用這種方式A.id *= B.id,最好都改用Left Join



Transact-SQL 聯結
在 Microsoft® SQL Server™ 2000 之前的版本中,左、右外部聯結 (Outer Join) 條件必須在 WHERE 子句中使用 *= 與=* 運算子來指定。部份情況中,這種語法可以使用多種方式解譯,可能會導致模糊不清的查詢。SQL-92 批評在 FROM 子句中指定外部聯結的語法而且 SQL-92 不會產生這種模稜兩可的情形,因為 SQL-92 語法更精確,有關在 WHERE 子句中舊的 Transact-SQL 外部聯結語法的詳細資訊並不包含在這個版本中。在未來 SQL Server 版本中可能不會支援這個語法。任何使用 Transact-SQL 外部聯結的陳述式都應該改為使用 SQL-92 的語法。

無論是 FROM 或 WHERE 子句中內部聯結的規格,SQL-92 標準並不支援。WHERE 子句中指定的內部聯結不會有如同 Transact-SQL 外部聯結語法中模稜兩可的相同問題。
leo_lesley 2007-04-05
  • 打赏
  • 举报
回复
Select A.id,A.cname,IsNull(B.charge, 0) As charge
From A , B
where b.note='加班' and
A.id *= B.id --- 这句的*号在那边就是显示那边的记录,也就是一那边的记录为主
FoxLinn 2007-04-05
  • 打赏
  • 举报
回复
楼上的,不行!
paoluo 2007-04-05
  • 打赏
  • 举报
回复
這麼修改即可

Select A.id,A.cname,IsNull(B.charge, 0) As charge
From A
Left Outer Join B
On A.id = B.id
And b.note='加班' -- Where 修改為And即可
加载更多回复(1)

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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