如何以MySQL选择查询的特定字段优先删除重复?

发布于 2025-01-19 11:50:09 字数 1070 浏览 1 评论 0原文

我有一个 mysql 查询,它返回一些重复的记录。当mysql结果集中同时存在“成功”和“错误”时,我需要获得唯一的记录,并且它应该只考虑“成功”作为优先级。在下面的示例中 - 作业名称“Timothy Plan Global CrRd”有两条记录 - 成功和错误。我只需要获得成功一项。我尝试在 job_name 上使用 group by 子句,但它只在结果集中拾取错误一。但我想要以成功者为优先的状态。你能帮忙吗?

这是查询 -

select status as 'Status', job_logid as 'Job Log ID', job_name as 'Job Name', job_type as 'Job Type', file_name as 'File Name', start_time as 'Run Date/Time', run_user as 'Run User' from etp.job_log
where DATE(start_time) = Date(now())-1 and job_type = 'Outbound'
and job_name like '%CrRd%'
order by job_name;

在此处输入图像描述

这是 David 的正确查询。谢谢。

select max(status) as 'Status', job_logid as 'Job Log ID', job_name as 'Job Name', job_type as 'Job Type', file_name as 'File Name', start_time as 'Run Date/Time', run_user as 'Run User' from etp.job_log
where DATE(start_time) = Date(now())-1 and job_type = 'Outbound'
and job_name like '%CrRd%'
group by job_name

I've a mysql query which returns few records with duplicates. I need to get the unique record when both "Success" and "Error" exist in mysql result set and it should consider only "Success" as precedence. In below example - Job name "Timothy Plan Global CrRd" has two records - Success and Error. I need to get only Success one. I tried with group by clause on job_name but it only picks up Error one in result set. But I want the status with success one as precedence. Can you please help?

Here is the query -

select status as 'Status', job_logid as 'Job Log ID', job_name as 'Job Name', job_type as 'Job Type', file_name as 'File Name', start_time as 'Run Date/Time', run_user as 'Run User' from etp.job_log
where DATE(start_time) = Date(now())-1 and job_type = 'Outbound'
and job_name like '%CrRd%'
order by job_name;

enter image description here

Here is the correct query by David. Thank you.

select max(status) as 'Status', job_logid as 'Job Log ID', job_name as 'Job Name', job_type as 'Job Type', file_name as 'File Name', start_time as 'Run Date/Time', run_user as 'Run User' from etp.job_log
where DATE(start_time) = Date(now())-1 and job_type = 'Outbound'
and job_name like '%CrRd%'
group by job_name

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有心的人 2025-01-26 11:50:09

我建议使用SELECT 选择最大(状态)为“状态”,这可以完成首先获得状态'成功'的工作。然后,我们可以使用job_logid,job_name 将组从没有错误的中保存重复项,并避免选择在成功状态之前o后出现错误的副本。

I suggest using select select max(status) as 'Status', that could do the job of getting first the status 'Success'. Then we can use group by job_logid,job_name to preserve the duplicates from the one that doesnt have an error and avoid to select the ones that have an error after o before a Success status.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文