时间:2021-07-01 10:21:17 帮助过:4人阅读
我试了这个语句:
SELECT DISTINCT a,b,c FROM `table`
结果不对,查出来的是3个字段都相同才排除
而我要的是3个字段中任意一个字段的值相同就排除
等于说实现的效果跟把这3个字段设置成唯一键值一样的导量,查询这3个字段的值都为唯一的结果
mysql中3个字段,如果这3个字段中,任何一个字段有重复值,则为重复,应该怎么写?
比如
a b c
1 2 3
4 5 3
c字段重复了,那么这两条记录就算重复。
我试了这个语句:
SELECT DISTINCT a,b,c FROM `table`
结果不对,查出来的是3个字段都相同才排除
而我要的是3个字段中任意一个字段的值相同就排除
等于说实现的效果跟把这3个字段设置成唯一键值一样的导量,查询这3个字段的值都为唯一的结果
但是你想查出什么呢…?比如1 2 3和1 4 3我应该留下哪个呢?这个查询的意义在哪里呢?
sql语句有点复杂:
SELECT
t1.a,
t1.b,
t1.c
FROM
demo AS t1
WHERE
t1.a NOT IN (
SELECT
a
FROM
demo
WHERE
t1.id != id
)
AND
t1.a NOT IN (
SELECT
b
FROM
demo
WHERE
t1.id != id
)
AND
t1.a NOT IN (
SELECT
c
FROM
demo
WHERE
t1.id != id
)
AND
t1.b NOT IN (
SELECT
a
FROM
demo
WHERE
t1.id != id
)
AND
t1.b NOT IN (
SELECT
b
FROM
demo
WHERE
t1.id != id
)
AND
t1.b NOT IN (
SELECT
c
FROM
demo
WHERE
t1.id != id
)
AND
t1.c NOT IN (
SELECT
a
FROM
demo
WHERE
t1.id != id
)
AND
t1.c NOT IN (
SELECT
b
FROM
demo
WHERE
t1.id != id
)
AND
t1.c NOT IN (
SELECT
c
FROM
demo
WHERE
t1.id != id
)
select distinct id
from (
select distinct a from table
union
select distinct b from table
union
select distinct c from table
) as t