34,872
社区成员
发帖
与我相关
我的任务
分享;with tb AS (
select 1 id, 1 A, 1A1, 2 B, 2 B1, 3 C, 3 C1,4 D,4 D1
UNION ALL select 2 id, 1 A, 1A1, 1 B, 1 B1, 3 C, 3 C1,4 D,4 D1
UNION ALL select 3 id, 1 A, 1A1, 2 B, 2 B1, 1 C, 1 C1,4 D,4 D1
UNION ALL select 4 id, 1 A, 1A1, 2 B, 2 B1, 3 C, 3 C1,1 D,1 D1
UNION ALL select 5 id, 1 A, 1A1, 2 B, 2 B1, 2 C, 2 C1,4 D,4 D1
UNION ALL select 6 id, 1 A, 1A1, 2 B, 2 B1, 3 C, 3 C1,3 D,3 D1)
--方法1
Select * from tb where not ( A+A1=B+B1 or A+A1 =C+C1 or A+A1 =D+D1 or B+B1 =C+C1 or B+B1 =D+D1 or C+C1=D+D1)
--方法2
SELECT * FROM TB WHERE ID IN(
SELECT id FROM (
select id,A+A1 AA from tb
UNION ALL
select id,B+B1 from tb
UNION ALL
select id,C+C1 from tb
UNION ALL
select id,D+D1 from tb ) tb
group by id having(count(DISTINCT AA)>=4)) Select * from tb where not ( A+A1=B+B1 or A+A1 =C+C1 or A+A1 =D+D1 or B+B1 =C+C1 or B+B1 =D+D1 or C+C1=D+D1)
--没有试过,不知道可不可以
select * from table where a+a1<>b+b1<>c+c1<>d+d1
--如果id 是主键的话
select * from tb a join (
select A+A1 A2 ,B+B1 B2 ,C+C1 C2 ,D+D1 D2,max(id)
from tb
group by A+A1,B+B1,C+C1,D+D1 ) b
on a.id = b.idselect * from tb a join (
select A+A1 A2 ,B+B1 B2 ,C+C1 C2 ,D+D1 D2
from tb
group by A+A1,B+B1,C+C1,D+D1 ) b
on a.A+A1 = A2 AND a.B+B1 = b.B2 AND a.C+C1 = c.C2 AND a.D+D1 = b.D2
select * from 表 where id in (select min(id) from 表 group by A+A1,B+B1,C+C1,D+D1 )