开发者

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 simple INNER 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜