简体   繁体   English

当另一个表中没有行时,如何从一个表中删除行

[英]How do I delete a row from one table, when no more rows exist in another

I would like to know the best way to check one table of data, and when no more rows exist matching the WHERE clause, delete a row from another table. 我想知道检查一个数据表的最佳方法,并且当不存在与WHERE子句匹配的更多行时,请从另一表中删除一行。

I have tried myself but it has become too cumbersome with 6 queries and nested if/else, and it doesn't work to top it off. 我已经尝试过了,但是6个查询变得非常麻烦,如果/ else则嵌套了,就无法成功了。

I have never used SQL join's before, so examples will help me to understand responses. 我以前从未使用过SQL连接,因此示例将帮助我理解响应。

I have a table of devices, there is a master table with a device and a password. 我有一个设备表,有一个带设备和密码的主表。

There is a second table containing the multiple rows of the device in the above table, and a series of serial numbers. 第二张表包含上表中设备的多行,以及一系列序列号。

When the second table no longer contains any of the serial numbers listed in the master table, I want the row containing the device and password from the master table. 当第二个表不再包含主表中列出的任何序列号时,我希望该行包含主表中的设备和密码。

If you mean like when you have a table customer and a table order, delete the customers if they have no orders? 如果您的意思是当您有一个表客户和一个表订单时,如果他们没有订单,将其删除吗? Then you can use subselect: 然后,您可以使用subselect:

delete from customer where customerid not in (select customerid from order)

You coult make a DELETE statement like 你coult作出这样一个DELETE语句

DELETE FROM masterTable WHERE ID NOT IN (SELECT masterTableID FROM secondaryTable)

This would delete all the rows from the master-table which don't have any references in the second table. 这将从主表中删除所有在第二张表中没有任何引用的行。 That also means it would not delete only one row, but all of the matching ones. 这也意味着它将不会只删除一行,而是删除所有匹配的行。 The only necessary thing you need is that every row in the second table references to the master table. 您唯一需要的是第二个表中的每一行都引用主表。

DELETE table_devices
FROM table_devices
left JOIN serial ON table_devices.id= serial.device_id
WHERE serial.device_id is null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果有4行或更多行以用户名命名,如何删除行 - How do I delete a row if there are 4 or more rows named after the username 当一个表的数据库中不存在行时如何插入记录 - How do i insert record when row is not exist in database within one table 我如何检查一个表中是否存在会话ID,而另一表中是否存在该ID,则回显错误或成功代码 - how do i check if session ID exist from one table exist in another table and echo out error or success code 如何删除SQLite表中的一行? - How do i delete one row in my SQLite table? 如何将行从一个MySQL表复制到另一个表并基于变量更新值之一 - How do I copy rows from one MySQL table to another and update one of the values based on a variable 如何从一个表中选择被MySQL中的另一个表过滤的行? - How do I select rows from one table filtered by another table in MySQL? 如何使用一行从同一mySQL表中提取其他多行 - How do I use one row to pull multiple other rows from the same mySQL table 如何在另一个表的一行中传递一个表的ID? - How I pass the id from one table in a row in another table? 从一个表中删除ROWS,然后在MYSQLI中插入到另一个表中 - Delete ROWS from one table and then INSERT into another TABLE in MYSQLI 将表中的许多行插入另一表中的唯一行 - Insert many rows from a table into one unique row in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM