【问题标题】:Join query but eliminate the duplicate加入查询但消除重复
【发布时间】:2020-07-13 01:36:52
【问题描述】:

所以我有这些数据,来自 tbl_left :

id   name   type    our_assets
1   'Mira'   cc        10
2   'Mira'   bb        9

还有这个,来自 tbl_right :

id   name   assets_other  name_of_assets
1   'Mira'      3            assets_a
2   'Mira'      1            assets_b
3   'Mira'      1            assets_c
4   'Mira'      1            assets_d

我怎样才能加入这两个表和结果是这样的:

 name   type    our_assets   assets_other  name_of_assets
'Mira'   cc        10            3            assets_a
'Mira'   bb        9             1            assets_b
'Mira'                           1            assets_c
'Mira'                           1            assets_d

我不在乎列类型和 our_assets 是否有重复值,我尝试使用 join 但结果变为 8 行而不是 4 行:

 name   type    our_assets   assets_other  name_of_assets
'Mira'   cc        10            3            assets_a
'Mira'   cc        10            3            assets_a
'Mira'   bb        9             1            assets_b
'Mira'   bb        9             1            assets_b
'Mira'                           1            assets_c
'Mira'                           1            assets_c
'Mira'                           1            assets_d
'Mira'                           1            assets_d

如果我使用 group by name_of_assets 列类型仅返回“cc”值。

【问题讨论】:

  • 到目前为止你尝试了什么?
  • SELECT a.name,a.type,a.our_assets,b.assets_other,b.name_of_assets FROM tbl_left a right join tbl_right b on a.name=b.name
  • SELECT a.name,a.type,a.our_assets,b.assets_other,distinct(b.name_of_assets) FROM tbl_left a right join tbl_right b on a.name=b.name 试试这个跨度>
  • @Hirumina 仍然返回 8 行..

标签: mysql sql join distinct


【解决方案1】:

您需要添加除name 之外的另一个连接条件以避免重复记录。一种选择使用row_number()(在 MySQL 8.0 中可用):

select r.name, l.type, l.our_assets, r.assets_other, r.name_of_asset
from (select r.*, row_number() over(partition by name order by id) rn from tbl_right) l
left join (select l.*, row_number() over(partition by name order by id) rn from tbl_left) r
    on r.name = l.name and r.rn = l.rn

如果连接的两端可能缺少记录,您可以使用union all 和聚合来代替:

select 
    name,
    max(type) type,
    amx(our_assets) our_assets,
    max(assets_other) assets_other,
    max(name_of_asset) name_of_asset
from (
    select 
        name, 
        type, 
        our_assets, 
        null assets_other, 
        null name_of_asset, 
        row_number() over(partition by name order by id) rn 
    from tbl_left
    union all
    select 
        name, 
        null, 
        null, 
        assets_other, 
        name_of_asset, 
        row_number() over(partition by name order by id) rn 
    from tbl_right
) t
group by name, rn

【讨论】:

    【解决方案2】:

    我想你想在idnameleft join

    select r.*, l.type, l.our_assets
    from tbl_right r left join
         tbl_left l
         on l.id = r.id and l.name = r.name
    

    【讨论】:

      猜你喜欢
      • 2011-05-03
      • 1970-01-01
      • 2018-06-15
      • 2020-08-09
      • 1970-01-01
      • 1970-01-01
      • 2016-09-14
      • 1970-01-01
      • 2015-12-21
      相关资源
      最近更新 更多