0

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.

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.

I have never used SQL join's before, so examples will help me to understand responses.

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.

5
  • I will post example code when I get home, its home time in AU right now, so it will be up later.
    – Gawdor
    Oct 19, 2012 at 6:36
  • At max, you need only 2 queries. 1st check if data exists in table a, return the result, if its empty then delete the row. Simple procedure.
    – itachi
    Oct 19, 2012 at 6:37
  • could you pls include some sample data together with your intended result for easier understanding?
    – sel
    Oct 19, 2012 at 6:37
  • The intended result is when a serial number is deleted from the second table, it checks if its the last row deleted (and no more exist for that device). If its the last row for that device deleted, it then goes and deletes the corresponding device row from the master table.
    – Gawdor
    Oct 19, 2012 at 6:39
  • You could redesign your database schema with foreign keys and the ON DELETE CASCADE option, then deletion from one table would automatically delete related records from all the linked tables
    – Mark Baker
    Oct 19, 2012 at 6:41

3 Answers 3

0

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:

delete from customer where customerid not in (select customerid from order)
4
  • Ok, I had no idea a query like that existed, but that could work perfectly. Would it be something like "DELETE from master WHERE device not in (SELECT device FROM serialtable WHERE device='abcd1234')
    – Gawdor
    Oct 19, 2012 at 6:40
  • Actually, I just realised on the drive home, if I add the where clause to that select it could very well delete everything from the master table ... oops.
    – Gawdor
    Oct 19, 2012 at 7:22
  • well yes be careful not to make a terrible mistake, but you can test it by doing a select with the same where clause first Oct 19, 2012 at 7:26
  • I tested it (I am still developing and emptying those tables a lot) and it worked perfectly as expected. Thanks for your help it took what I had (approx 30 lines of nested if statements) down to 2 lines (PDO MySQL queries).
    – Gawdor
    Oct 19, 2012 at 7:44
0

You coult make a DELETE statement like

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.

2
  • Thanks boldty your answer is the same as Jeroen's above, and works perfectly as expected.
    – Gawdor
    Oct 19, 2012 at 7:45
  • Glad I could maybe help a little :-)
    – mboldt
    Oct 19, 2012 at 7:53
0
DELETE table_devices
FROM table_devices
left JOIN serial ON table_devices.id= serial.device_id
WHERE serial.device_id is null
2
  • That reads similar to the previous two answers, is it's sanity (coding sanity) any better than the others?
    – Gawdor
    Oct 19, 2012 at 7:47
  • i just wrote an alternative method .
    – Arun Killu
    Oct 19, 2012 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.