Mysql根据一列删除重复记录,并保留最长的行

时间:2018-03-07 00:25:02

标签: mysql sql

大家好我有以下代码来获取每一行的长度:

SELECT member_id, 
      (sum(length(first_name) + length(last_name) 
        + length(email) + length(mobile_phone))) as size
FROM my_table
GROUP BY member_id
ORDER BY size desc;

由于我有很多具有相同电子邮件的记录,我想删除重复内容,只保留最长记录(表示该记录中包含大部分信息)。那怎么办?

示例数据:

from:
    +------+--------+---------+-------+-------+
    | id   | name   | Surnmae | email |address|
    +------+--------+---------+-------+-------+
    | 1    |        | Lee     | aaa   |23 a st|
    | 2    | a      |         | aaa   |       |
    | 3    | c      |         | ccc   |       |
    +------+--------+---------+-------+-------+

to:
    +------+--------+---------+-------+-------+
    | id   | name   | Surnmae | email |address|
    +------+--------+---------+-------+-------+
    | 1    |        |  Lee    | aaa   |23 a st|
    | 3    | c      |         | ccc   |       |
    +------+--------+---------+-------+-------+

id 1有更多信息(更长的长度),这就是为什么它被保留。

2 个答案:

答案 0 :(得分:0)

我的mySql版本是5.6,这个查询有效。如果没有,请告诉我并为您找到答案。感谢。

select * 
from my_table 
where member_id in (
SELECT member_id 
from (
 SELECT member_id,
       email,
      (sum(length(first_name) + length(last_name) 
        + length(email) + length(mobile_phone))) as size
FROM my_table 
GROUP BY member_id, email) m
join (SELECT email, max(size) as sz
from (
SELECT member_id,email,
      (sum(length(first_name) + length(last_name) 
        + length(email) + length(mobile_phone))) as size
FROM my_table
GROUP BY member_id,email
    ) t 
    GROUP BY email
 ) s on m.email=s.email and m.size =s.sz
 ) 

    Result:
member_id   first_name  last_name   email   mobile_phone
2                       Lee         aaa     168-168
3           c                       ccc     888-888

答案 1 :(得分:0)

我想我会使用变量:

select t.*
from t
where (t.name is not null) + (t.surname is not null) + (t.address is not null) = 
          (select max( (t2.name is not null) + (t2.surname is not null) + (t2.address is not null) )
           from t t2
           where t2.email = t.email
          );