急求一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
select 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