sql easy

    196. 删除重复的电子邮箱

    1. delete from person where id not in
    2. (
    3. select * from
    4. (
    5. select MIN(id) from person group by email
    6. ) t
    7. );

    这道题, 涉及到一个很重要的知识点:

    如果我们直接筛选 id 然后删除:

    1. delete from person
    2. where id not in
    3. (select MIN(id) from person group by email);

    会报错: You can't specify target table 'person' for update in FROM clause. 这个错误的意思是不能在同一个 sql 语句中, 先 select 同一个表的某些值, 然后再 update 这个表.
    解决方法: select 的结果再通过一个中间表 select 多一次, 就可以避免这个错误. 目前遇到 update 也会这样,其他的 DML 语句后续遇到了再补充.