加入查询但消除重复
Join query but eliminate the duplicate
所以我有这些数据,来自 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
我怎样才能加入两者 table 并得到类似这样的结果:
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 列类型仅 return "cc" 值。
您需要添加除 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
我想你想 left join
在 id
和 name
:
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
所以我有这些数据,来自 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
我怎样才能加入两者 table 并得到类似这样的结果:
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 列类型仅 return "cc" 值。
您需要添加除 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
我想你想 left join
在 id
和 name
:
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