cuốn sách gpt4 ai đã làm

mysql - 我可以执行 mysql 命令来过滤和删除重复条目吗

In lại Tác giả: Walker 123 更新时间:2023-11-29 05:30:47 27 4
mua khóa gpt4 Nike

我将表作为liên kết,具有以下值

++++++++++++++++++++++++++
+ company_id + industry +
++++++++++++++++++++++++++
+ 1 + a +
+ 1 + b +
+ 2 + a +
+ 2 + c +
+ 3 + a +
+ 4 + c +
+ 5 + a +
++++++++++++++++++++++++++

有没有一种方法可以对我的行业进行分组,以按 desc 订单示例获得最高计数。

a = count 4
c = count 2
b = count 1

然后删除重复的行业,只留下每个 company_id 计数较高的行业。


编辑1

此编辑基于 OP 评论 我希望只有计数最高的行业,并删除同一 company_id 的其余条目。对于 company_id 1,我们将删除第二行,对于 company_id 2,我们将删除第四行。

下面是我的。

++++++++++++++++++++++++++
+ company_id + industry +
++++++++++++++++++++++++++
+ 1 + a +
+ 1 + b +
+ 1 + c +
+ 2 + a +
+ 2 + c +
+ 3 + a +
+ 4 + c +
+ 5 + a +
++++++++++++++++++++++++++

正如我们在专栏 industry 中看到的那样,a 具有最大计数,我想为每个重复的 company_id 保留此条目并删除其余所有条目。

考虑 company_id=1。我需要删除第二行和第三行。考虑 company_id=2。我需要删除第五行。对于 id=3,4,5 不会发生任何事情,因为它们没有重复。

所以我表中应该有的最终数据是

++++++++++++++++++++++++++
+ company_id + industry +
++++++++++++++++++++++++++
+ 1 + a +
+ 2 + a +
+ 3 + a +
+ 4 + c +
+ 5 + a +
++++++++++++++++++++++++++

câu trả lời hay nhất

select t6.company_id,t6.industry from
(select t5.company_id,t5.industry,
row_number() over (partition by t5.company_id order by t5.company_id) rn
từ
(select t3.company_id,t4.industry from
(select t2.company_id,max(t2.count) count from(
select m.company_id,m.industry,t1.count from linkage m
join
(select n.industry,count(n.industry) count from linkage n
group by n.industry
order by count desc)t1
on m.industry = t1.industry
order by m.company_id)t2
group by t2.company_id
order by t2.company_id)t3
join
(
select m.company_id,m.industry,t1.count from linkage m
join
(select n.industry,count(n.industry) count from linkage n
group by n.industry
order by count desc)t1
on m.industry = t1.industry
order by m.company_id)t4
on t3.company_id = t4.company_id
and t3.count = t4.count)t5
)t6
where t6.rn = '1'

关于mysql - 我可以执行 mysql 命令来过滤和删除重复条目吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14996202/

27 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress