Delete from a table according to another table
I want to delete records from a table according to another table. for example this is the base table:
table1:
License Major
9 0012
8 0015
7 0018
9 0019
and I want to delete items in table1 according to table开发者_C百科2:
table2:
License Major
8 0015
7 0018
9 0019
something like this:
delete from table1
where table1.license=table2.license
and table1.major=table2.major
The DELETE FROM syntax comes in very handy here.
- the second
FROM
clause is a simpleINNER JOIN
selecting all the matching records. - the first
FROM
clause deletes everything from table1 that matches the second clause.
SQL Statement
DELETE FROM t1
FROM table1 t1
INNER JOIN table2 t2 ON t2.License = t1.License
AND t2.Major = t1.Major
(Also works)
delete table1
from table2
where table1.license=table2.license and table1.major=table2.major
精彩评论