(不使用ISNULL()来设定固定值)
5 个解决方案
#1
select isnull(b.Cate_no,'CL2-L1') as Cate_no,a.Size_no,a.Size_name,isnull(b.Qty,0) as Qty from 表A as a left join 表B as b on a.Size_no=b.Size_no
如果不用isnull()设定固定值,可以采用下面的方法。
select 'CL2-L1' as Cate_no,a.*,0 as Qty into #temp from 表A as a
update #temp set Qty=b.Qty from 表B as b where Cate_no=b.Cate_no and Size_no=b.Size_no
select * from #temp
#2
INSERT INTO b
( cate_no
,size_no
,size_name
,Qty
)
SELECT x.cate_no
,size_no
,size_name
,0 AS Qty
FROM ( SELECT DISTINCT
cate_no
FROM b
) AS x
CROSS JOIN a AS y
WHERE NOT EXISTS ( SELECT 1
FROM b
WHERE cate_no = x.cate_no
AND size_no = y.size_no )
SELECT * FROM b
#3
create table 表A(Size_no varchar(5),Size_name int)
create table 表B(Cate_no varchar(10),Size_no varchar(5),Size_name int,Qty int)
insert into 表A
select 'S1',1 union all
select 'S2',2 union all
select 'S3',3 union all
select 'S4',4 union all
select 'S5',5 union all
select 'S6',6 union all
select 'S7',7 union all
select 'S8',8 union all
select 'S9',9 union all
select 'S10',10
insert into 表B
select 'CL2-L1','S2',2,500 union all
select 'CL2-L1','S5',5,300 union all
select 'CL2-L1','S6',6,400 union all
select 'CL2-L1','S8',8,345
select b.Cate_no,
a.Size_no,
a.Size_name,
isnull(c.Qty,0) 'Qty'
from (select distinct Cate_no from 表B) b
cross join 表A a
left join 表B c on b.Cate_no=c.Cate_no and a.Size_no=c.Size_no
/*
Cate_no Size_no Size_name Qty
---------- ------- ----------- -----------
CL2-L1 S1 1 0
CL2-L1 S2 2 500
CL2-L1 S3 3 0
CL2-L1 S4 4 0
CL2-L1 S5 5 300
CL2-L1 S6 6 400
CL2-L1 S7 7 0
CL2-L1 S8 8 345
CL2-L1 S9 9 0
CL2-L1 S10 10 0
(10 行受影响)
*/
#4
我现在也是这么写的,想知道有没更好的方法呢?
#5
--就结果而言,你参考下:
select distinct a.size_no,a.size_name,'CL2-L1' as cate_no,isnull(b.qty,0) as qty from 表A as a left join 表B as b
on a.size_no=b.size_no order by size_name
#1
select isnull(b.Cate_no,'CL2-L1') as Cate_no,a.Size_no,a.Size_name,isnull(b.Qty,0) as Qty from 表A as a left join 表B as b on a.Size_no=b.Size_no
如果不用isnull()设定固定值,可以采用下面的方法。
select 'CL2-L1' as Cate_no,a.*,0 as Qty into #temp from 表A as a
update #temp set Qty=b.Qty from 表B as b where Cate_no=b.Cate_no and Size_no=b.Size_no
select * from #temp
#2
INSERT INTO b
( cate_no
,size_no
,size_name
,Qty
)
SELECT x.cate_no
,size_no
,size_name
,0 AS Qty
FROM ( SELECT DISTINCT
cate_no
FROM b
) AS x
CROSS JOIN a AS y
WHERE NOT EXISTS ( SELECT 1
FROM b
WHERE cate_no = x.cate_no
AND size_no = y.size_no )
SELECT * FROM b
#3
create table 表A(Size_no varchar(5),Size_name int)
create table 表B(Cate_no varchar(10),Size_no varchar(5),Size_name int,Qty int)
insert into 表A
select 'S1',1 union all
select 'S2',2 union all
select 'S3',3 union all
select 'S4',4 union all
select 'S5',5 union all
select 'S6',6 union all
select 'S7',7 union all
select 'S8',8 union all
select 'S9',9 union all
select 'S10',10
insert into 表B
select 'CL2-L1','S2',2,500 union all
select 'CL2-L1','S5',5,300 union all
select 'CL2-L1','S6',6,400 union all
select 'CL2-L1','S8',8,345
select b.Cate_no,
a.Size_no,
a.Size_name,
isnull(c.Qty,0) 'Qty'
from (select distinct Cate_no from 表B) b
cross join 表A a
left join 表B c on b.Cate_no=c.Cate_no and a.Size_no=c.Size_no
/*
Cate_no Size_no Size_name Qty
---------- ------- ----------- -----------
CL2-L1 S1 1 0
CL2-L1 S2 2 500
CL2-L1 S3 3 0
CL2-L1 S4 4 0
CL2-L1 S5 5 300
CL2-L1 S6 6 400
CL2-L1 S7 7 0
CL2-L1 S8 8 345
CL2-L1 S9 9 0
CL2-L1 S10 10 0
(10 行受影响)
*/
#4
我现在也是这么写的,想知道有没更好的方法呢?
#5
--就结果而言,你参考下:
select distinct a.size_no,a.size_name,'CL2-L1' as cate_no,isnull(b.qty,0) as qty from 表A as a left join 表B as b
on a.size_no=b.size_no order by size_name