按特定ID删除一行,其中不存在另一个表列ID

时间:2016-07-04 13:58:56

标签: mysql sql

mysql> select *from add_member;
+-----------+-------------+--------------+-------------------+
| member_ID | member_name | member_phone | member_department |
+-----------+-------------+--------------+-------------------+
|     12345 | kishore     | 89482934     | IT738             |
|     12347 | ram         | 783892389    | IT721829          |
|    12346 | vardhan     | 902112       | TI5738            |

mysql> select *from ownbook;


    +--------+----------+-----------+-------------+------------+-----------

    | BookID | Bookname | member_ID | member_name | from       | to         |
    +--------+----------+-----------+-------------+------------+------------+
    |   1006 | physics  |     12346 | ram         | 2016-07-05 | 2016-07-20 |
    +--------+----------+-----------+-------------+------------+------------+
1 row in set (0.00 sec)

2 个答案:

答案 0 :(得分:1)

您可以尝试这样:

delete from add_member where member_ID  not exists(select member_ID from ownbook)

或者你可以像使用NOT IN一样使用

delete from add_member where member_ID  not in(select member_ID from ownbook)

如果是特定数据,那么只需:

delete from add_member 
where member_ID  not in(select member_ID from ownbook where member_id in(12345,12347))

答案 1 :(得分:0)

如果您想要删除一个特定的ID,当且仅当它不在第二个表中时,那么您可以这样做:

delete from add_member where member_ID='SEPCIFIC_ID_YOU_WANT' and member_ID not in(select member_ID from ownbook)

100%经过测试。