V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
cnmllll
V2EX  ›  MySQL

关于 sql 查询和删除....请教!

  •  
  •   cnmllll · 2018-07-20 19:13:10 +08:00 · 3034 次点击
    这是一个创建于 2078 天前的主题,其中的信息可能已经有所发展或是发生改变。

    select A.ID from A left join B on A.ID=B.ID where B.ID is null

    1.我用以上语句搜出了和 B 表未关联的 A.ID

    2.请问我该如何写 sql 才能直接删除这些 A.ID ,既一条语句删除查出来未关联的 ID

    8 条回复    2018-07-20 22:43:44 +08:00
    cnmllll
        1
    cnmllll  
    OP
       2018-07-20 19:20:47 +08:00
    delete from A.ID where A.ID not not in (select A.ID from A left join B on A.ID=B.ID where B.ID is null)
    这样可以么
    cnmllll
        2
    cnmllll  
    OP
       2018-07-20 19:21:13 +08:00
    delete from A where A.ID not not in (select A.ID from A left join B on A.ID=B.ID where B.ID is null)
    这个
    jintianfengda
        3
    jintianfengda  
       2018-07-20 19:21:31 +08:00
    DELETE
    a
    FROM
    a
    LEFT JOIN
    b
    ON
    a.ID = b.ID
    WHERE
    b.id is null
    cnmllll
        4
    cnmllll  
    OP
       2018-07-20 19:35:51 +08:00
    delete from A where A.ID in (select A.ID from A left join B on A.ID=B.ID where B.ID is null) 这个终极版
    akira
        5
    akira  
       2018-07-20 19:39:52 +08:00   ❤️ 1
    这个 sql 和 select id from A where id is null 有啥区别
    lrh3321
        6
    lrh3321  
       2018-07-20 19:48:39 +08:00
    @akira 表 A 中的行在表 B 中不一定有对应的行,他要删的就是这些无用的行。
    akira
        7
    akira  
       2018-07-20 19:51:38 +08:00
    @lrh3321 嗯呐,审题不仔细 >"<
    iwiki
        8
    iwiki  
       2018-07-20 22:43:44 +08:00
    delete from a where not exists (select 1 from b where a.id=b.id)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1034 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:26 · PVG 06:26 · LAX 15:26 · JFK 18:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.