22,206
社区成员
发帖
与我相关
我的任务
分享
--假设
tb1
id
35码
36码
tb2
name id
A 35码
B 36码
create table tb1(id varchar(10))
insert into tb1 values('35码')
insert into tb1 values('36码')
create table tb2(name varchar(10) , id varchar(10))
insert into tb2 values('A' , '35码')
insert into tb2 values('B' , '36码')
go
select m.* , 是否有 = case when exists (select 1 from tb2 n where m.name = n.name and m.id = n.id) then 1 else 0 end from
(select distinct tb2.name , tb1.id from tb2 , tb1 ) m
drop table tb1 , tb2
/*
name id 是否有
---------- ---------- -----------
A 35码 1
A 36码 0
B 35码 0
B 36码 1
(所影响的行数为 4 行)
*/
select c,m,max(h) as h from(
select a.c,b.m,case when a.m=b.m then 1 else 0 end as h from(
select 'A'as c,'35码' as m union
select 'B','36码' )a ,
(
select '35码' as m union
select '36码')b)t
group by c,m
/*
c m h
---- ---- -----------
A 35码 1
B 35码 0
A 36码 0
B 36码 1
(4 行受影响)*/
select a.产品,
b.码数,
case when a.产品 is null then 0 else 1 end 是否有
from 码数基本表 b full join 产品 a on b.码数 = a.码数
-- 大概case when 就是你想要的...