I think this is newbie ask. How to delete row if id doesn't exist in other table? My thought is fetch the ids first and delete. I'm looking for better querying.
Deleting rows based on another table. Sometimes we need to delete rows based on another table. This table might exist in the same database or not. We can use the table lookup method or SQL join to delete these rows.
Example - DELETE Statement with One ConditionIf you run a DELETE statement with no conditions in the WHERE clause, all of the records from the table will be deleted. As a result, you will most often include a WHERE clause with at least one condition in your DELETE statement.
Use the delete command to delete blank rows in MySQL. delete from yourTableName where yourColumnName=' ' OR yourColumnName IS NULL; The above syntax will delete blank rows as well as NULL row.
You can use the WHERE clause with a DELETE query to delete the selected rows, otherwise all the records would be deleted.
You can set foreign keys to these tables.
More info here
for simple query, here it is:
DELETE FROM table WHERE (SELECT count(1) FROM table2 WHERE id = table.id) < 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With