读书人

急求1sql语句解决后立马给分!

发布时间: 2013-04-02 12:35:26 作者: rapoo

急求一sql语句,解决后立马给分!!!!
链表查询,

表A
字段:
webid code regnum
232 1143 23
242 1143 4
200 1143 21
212 1143 7
221 1143 17
111 1143 12
101 1143 6
231 1143 7
265 1143 9

表B
字段:
webid code hynum
232 1143 2
242 1143 8
200 1143 21
212 1143 7


要查询结果为:

webid code regnum hynum
232 1143 23 2
242 1143 4 8
200 1143 21 21
212 1143 7 7
221 1143 17 0
111 1143 12 0
101 1143 6 0
231 1143 7 0
265 1143 9 0


求sql语句



[解决办法]
select a.*,b.hynum from a left join b on a.webid=b.webid
[解决办法]
select a.*,isnull(b.hynum,0)hynum
from a left join b on a.webid=b.webid and a.code=b.code
[解决办法]
select a.*, b.hynum
from a left join b on a.webid=b.webid and a.code=b.code
[解决办法]

if object_id('A') is not null
drop table A
create table A
(
webid int primary key ,
code int ,
regnum int
)
if object_id('B') is not null
drop table B
create table B
(
webid int primary key ,
code int ,
hynum int
)
insert A
select 232,1143,23 union all
select 242,1143,4 union all
select 200,1143,21 union all
select 212,1143,7 union all


select 221,1143,17 union all
select 111,1143,12 union all
select 101,1143,6 union all
select 231,1143,7 union all
select 265,1143,9

insert B
select 232,1143,2 union all
select 242,1143,8 union all
select 200,1143,21 union all
select 212,1143,7

--左连接
select A.webid,A.code,A.regnum,hynum=isnull(B.hynum,0) from A left join B on A.webid = B.webid


[解决办法]

[解决办法]
赞成5楼的意见
[解决办法]
select A.webid,A.code,A.regnum,isnull(B.hynum,0) as hynum
from A left join B
on A.webid = B.webid

左链接就可以了
[解决办法]
select a.*,isnull(b.hynum,0) from a join b on a.webid=b.webid and a.code=b.code

[解决办法]
引用:
SQL code?1select a.*,isnull(b.hynum,0) from a join b on a.webid=b.webid and a.code=b.code


select a.*,isnull(b.hynum,0) from a left join b on a.webid=b.webid and a.code=b.code

读书人网 >SQL Server

热点推荐