我想检查该行是否有任何重复记录。 按表结构表示
if(C1_CADNO = C2_CADNO或C1_CADNO = C3_CADNO或C2_CADNO = C3_CADNO) 然后 显示该记录
我的表有10列,如C1_CADNO ..... C10_CADNO
答案 0 :(得分:2)
试试这个:
SELECT * FROM T WHERE
C1_CADNO IN (C2_CADNO,C3_CADNO,C4_CADNO,...C10_CADNO)
OR
C2_CADNO IN (C3_CADNO,C4_CADNO,...C10_CADNO)
OR
C3_CADNO IN (C4_CADNO,...C10_CADNO)
OR
C4_CADNO IN (C5_CADNO,...C10_CADNO)
...
OR
C8_CADNO IN (C9_CADNO,...C10_CADNO)
OR
C9_CADNO = C10_CADNO
答案 1 :(得分:1)
将其移至WHERE
子句:
SELECT t.* FROM TableName t
WHERE ( C1_CADNO = C2_CADNO
OR C1_CADNO = C3_CADNO
OR C2_CADNO = C3_CADNO )
但我的桌子有10列像 C1_CADNO,C2_CADNO,C3_CADNO ..... C10_CADNO -
然后这个SQL就是为你而生的:
SELECT t.* FROM TableName t
WHERE ( t.C1_CADNO = t.C2_CADNO
OR C1_CADNO = C3_CADNO
OR C1_CADNO = C4_CADNO
OR C1_CADNO = C5_CADNO
OR C1_CADNO = C6_CADNO
OR C1_CADNO = C7_CADNO
OR C1_CADNO = C8_CADNO
OR C1_CADNO = C9_CADNO
OR C1_CADNO = C10_CADNO
OR C2_CADNO = C3_CADNO
OR C2_CADNO = C4_CADNO
OR C2_CADNO = C5_CADNO
OR C2_CADNO = C6_CADNO
OR C2_CADNO = C7_CADNO
OR C2_CADNO = C8_CADNO
OR C2_CADNO = C9_CADNO
OR C2_CADNO = C10_CADNO
OR C3_CADNO = C4_CADNO
OR C3_CADNO = C5_CADNO
OR C3_CADNO = C6_CADNO
OR C3_CADNO = C7_CADNO
OR C3_CADNO = C8_CADNO
OR C3_CADNO = C9_CADNO
OR C3_CADNO = C10_CADNO
OR C4_CADNO = C5_CADNO
OR C4_CADNO = C6_CADNO
OR C4_CADNO = C7_CADNO
OR C4_CADNO = C8_CADNO
OR C4_CADNO = C9_CADNO
OR C4_CADNO = C10_CADNO
OR C5_CADNO = C6_CADNO
OR C5_CADNO = C7_CADNO
OR C5_CADNO = C8_CADNO
OR C5_CADNO = C9_CADNO
OR C5_CADNO = C10_CADNO
OR C6_CADNO = C7_CADNO
OR C6_CADNO = C8_CADNO
OR C6_CADNO = C9_CADNO
OR C6_CADNO = C10_CADNO
OR C7_CADNO = C8_CADNO
OR C8_CADNO = C9_CADNO
OR C8_CADNO = C10_CADNO
OR C9_CADNO = C10_CADNO
)
现在你知道为什么它对normalize表很重要了; - )
答案 2 :(得分:1)
不要枚举所有排列,只需计算行中的不同值。如果计数太小,那么你必须有重复。
SELECT *
FROM T
WHERE (
SELECT COUNT(DISTINCT value)
FROM (VALUES (C1_CADNO),(C2_CADNO),(C3_CADNO),(C4_CADNO),(C5_CADNO),(C6_CADNO),(C7_CADNO),(C8_CADNO),(C9_CADNO),(C10_CADNO) ) t(value)
) < 10
答案 3 :(得分:0)
这会有效,
select CADNO,count(*) cnt from
(select C1_CADNO [CADNO] from table1
union all
select C2_CADNO from table1
......
....
select C10_CADNO from table1
)t4 group by CADNO
having count(*)>1