且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何删除mysql查询中的重复项

更新时间:2023-02-13 16:17:25

这不是关于删除重复项,因为第一个结果集中的记录都不是另一个记录的重复项.您希望将所有字段的 'P' 值合并为一条记录.您可以使用 group by 和 max() 函数来达到预期的结果:

This is not about removing duplicates, since none of the records in the first resultset is a duplicate of another one. You want to merge the 'P' values for all fields into a single record. You can use group by and max() function to achive the expected outcome:

select org_id,  org_emp_id,  name, max(day1) as day1, ... , max(day30) as day30
from yourtable
group by org_id,  org_emp_id,  name

您需要列出所有 dayN 字段来代替上述查询中的 ....如果 dayN 字段的所有值都是N/A",则 max() 将返回N/A".如果有P"值,则 max() 将返回P",因为字母 P 的字符代码高于 N.

You need to list all dayN fields in place of the ... in the above query. If for a dayN field all values are 'N/A', then max() will return 'N/A'. If there is a 'P' value, then max() will return the 'P', since letter P has higher character code, than N.