【问题标题】:SQL Query to select row having two or more identical columnsSQL 查询以选择具有两个或多个相同列的行
【发布时间】:2019-10-07 06:12:28
【问题描述】:

我有桌子供应商

    line    name     country    supplier     city
    1        A         UK         Google     London
    1        A         UK         Apple      London
    1        A         UK         Amazon     London
    2        B         UK         Microsoft  London
    3        C         UK         Amazon     London
    4        D         UK         Google     Oxford
    4        D         UK         Apple      Oxford
    4        D         UK         Amazon     Oxford

我希望创建一个案例声明,如果相同的行号出现多次,它将选择特定的供应商,在这种情况下,如果城市是伦敦,则选择“Google”,否则如果城市是牛津和同一行出现多次,会选择供应商“亚马逊”

预期结果

    line    name     country    supplier     city
    1        A         UK         Google     London
    2        B         UK         Microsoft  London
    3        C         UK         Amazon     London
    4        D         UK         Amazon     Oxford

【问题讨论】:

  • 你的主键是什么
  • 行是我的主键
  • 一些简单的解决方案需要使用主键进行连接。 line 不能是您的主键,因为它不是唯一的。

标签: mysql sql sorting duplicates


【解决方案1】:

您可以使用相关子查询:

select t.*
from t
where (supplier, city) = (select t2.supplier, t2.city
                          from t t2
                          where t2.line = t.line  -- and perhaps other conditions as well
                          order by (case when (supplier, city) in ('Google', 'London') then 1
                                         when (supplier, city) in ('Amazon', 'Oxford') then 1
                                         else 3
                                    end)
                          limit 1
                         );

您可以轻松地向order by 子句添加更多优先级条件。

【讨论】:

    【解决方案2】:

    您可以使用以下查询

    select *
      from
    (
     select line, name, country, max(supplier), city
       from tab
      group by line, name, country, city
     having count(supplier)=1
    union all
    select line, name, country, 
           case when city = 'London' then max(supplier) 
                when city = 'Oxford' then min(supplier)
            end supplier, city
      from tab
     group by line, name, country, city
     having count(supplier)>1 
     ) q
     order by q.line;
    

    Demo

    【讨论】:

    • 如果桌子更长,有更多城市和 5 个供应商在一条线上,会发生什么?最大值和最小值还会如此吗?
    • @cshion 如果你准备了这样的样本来得到这样的结果怎么办?
    【解决方案3】:

    将表加入到由您设置条件的行组成的查询中,然后按行、名称、国家/地区、城市分组:

    select 
      t.line,
      t.name,
      t.country,
      coalesce(max(u.supplier), max(t.supplier)) supplier,
      t.city
    from tablename t left join (
      select 'Google' supplier, 'London' city 
      union all 
      select 'Amazon', 'Oxford'
    ) u on u.supplier = t.supplier and u.city = t.city 
    group by t.line, t.name, t.country, t.city
    

    请参阅demo
    结果:

    > line | name | country | supplier  | city  
    > ---: | :--- | :------ | :-------- | :-----
    >    1 | A    | UK      | Google    | London
    >    2 | B    | UK      | Microsoft | London
    >    3 | C    | UK      | Amazon    | London
    >    4 | D    | UK      | Amazon    | Oxford
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-17
      • 2016-05-21
      • 2021-03-18
      • 2014-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-11
      相关资源
      最近更新 更多