嗨,我最近开始做软件工程师,所以这个问题可能太麻烦了,但是请耐心点。
我有三根柱子,比如说水果,颜色,味道。
水果可以有多种颜色和口味。但是有特定颜色的水果应该有相同的味道。例如,(苹果,红色)应该只有一种口味。(苹果,红,酸)和(苹果,红,甜)不能共存。
我的问题是,我的桌子包含多重重复的口味,同样的水果和颜色。我尝试过不同的连接,但得到了错误的结果。
很抱歉有这么糟糕的描述,但是如果有人能理解和帮助我,我将非常感激。
发布于 2018-05-01 19:30:48
要查找对同一颜色具有多种口味的水果:
select  fruit
from    fruit
group by
        fruit
,       color
having  count(distinct taste) > 1列出与这些水果相关的所有行:
select  *
from    (
        select  count(*) over (partition by fruit, color) cnt
        ,       *
        from    fruits
        group by
                fruit
        ,       color
        ,       taste
        ) sub
where   cnt > 1SQL中的工作示例。
发布于 2018-05-01 19:28:16
使用row_number()函数
select * from (select *,
                    row_number() over (partition by fruit, taste order by fruit) Seq 
               from table t) t 
where Seq > 1;通过这个,您发现Seq >1,如果水果有重复的味道,您可以过滤它们&使用subquery和delete状态删除它们。
delete t 
from  (select *,
           row_number() over (partition by fruit, taste order by fruit) Seq 
       from table t) t
where Seq > 1;发布于 2018-05-01 19:31:54
对水果、颜色设置PK或唯一约束
select * 
from (select *, count(*) over (partition by fruit, color) as cnt 
      from table 
     ) t 
where t.cnt > 1 
order by fruit, colorhttps://stackoverflow.com/questions/50122563
复制相似问题