您的位置首页生活百科

一个SQL问题

一个SQL问题

select case t1.code when 'H' then 'H' else null end as HCode, case t2.code when 'L' then 'L' else null as LCode, t1.name from table as t1 join table as t2 on t1.name = t2.name;

select id, name from t1 where not exists(select * from t2 where id = t1.id and name = t1.name)

1.

select

(case when Code = 'H' then Code else NULL end) as HCode,

(case when Code = 'L' then Code else NULL end) as LCode,

Name

from 表

2.

select * from t1 where exists(select 1 from t2 where t1.id = t2.id and t1.name = t2.name)

1.用case解决:

select HCode=case Code when 'H' then 'H' end,

LCode=case Code when 'L' then 'L' end, Name

from tab

2.用not in解决:

select * from tab1

where id not in (select id from tab2)

1. select HCde,Lcode,name from (select code as HCode,name from table1 where code='H') a

out join (select code as Lcode,name from table1 where code='L') b on a.Hcode=b.Lcode

order by name

2.select * form T1 where T1.id not in(select id from T2)