美文网首页
SQL 重复数据插入

SQL 重复数据插入

作者: 沈三公子 | 来源:发表于2021-09-06 15:29 被阅读0次

    使用NOT EXISTS:

    INSERT INTO TABLE_2
    (id, name)
    SELECT t1.id,
    t1.name
    FROM TABLE_1 t1
    WHERE NOT EXISTS(SELECT id
    FROM TABLE_2 t2
    WHERE t2.id = t1.id)
    使用NOT IN:

    INSERT INTO TABLE_2
    (id, name)
    SELECT t1.id,
    t1.name
    FROM TABLE_1 t1
    WHERE t1.id NOT IN (SELECT id
    FROM TABLE_2)
    使用LEFT JOIN/IS NULL:

    INSERT INTO TABLE_2
    (id, name)
    SELECT t1.id,
    t1.name
    FROM TABLE_1 t1
    LEFT JOIN TABLE_2 t2 ON t2.id = t1.id
    WHERE t2.id IS NULL

    相关文章

      网友评论

          本文标题:SQL 重复数据插入

          本文链接:https://www.haomeiwen.com/subject/dqoewltx.html