Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql Subtract a table from another

Tags:

mysql

I have two tables: A contains all the data, table B created from A selecting %25 of its data randomly. So A and B have exact same columns. Also there is no unique column.

What I want to do is subtract B from A. Any ideas?

like image 859
osmanabi Avatar asked Apr 21 '11 00:04

osmanabi


1 Answers

To view all rows in A except those in B:

SELECT * FROM A
WHERE (field1, field2, ..., fieldN) NOT IN
( SELECT *
  FROM B
) ;

To actually delete from table A the rows that are in B:

DELETE FROM A
WHERE (field1, field2, ..., fieldN) IN
( SELECT *
  FROM B
) ;
like image 136
ypercubeᵀᴹ Avatar answered Sep 21 '22 11:09

ypercubeᵀᴹ