如何以MySQL选择查询的特定字段优先删除重复?
我有一个 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;
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用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 usegroup 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.