With PHP, compare two single-col MYSQL tables, adding records from table A that do not exist in table B, and deleting records that do -


There are two tables I have dB, A and B. Each table contains only one column, email. Therefore, I want to check every email in Table A in Table A, and if it is present then delete it; If it does not exist then add it.

How do I do this?

Ideally, you should do it in pure SQL, suck with PHP without it.

Let's say your table A is an integer and table column B int . If you can do something like this:

  temporary table X Create, Connect with A B A = B. B; Remove from B where in B (choose from X); Remove from A (choose one from X); Put in A (A) (choose B to B);   

It does the following:

  1. Creates a temporary table and enter all records that are present in both A and B deletes all records in both tables <

    table X temporary Designed to be built, it will be automatically deleted when the database connection stops.

    Now, if you have to call it with PHP, you can do the following: $ db = new PDO ($ CONNECT_STRING, USERNAME, password); $ Db- & gt; Choose from the joining on exec ("a.b. b." to create temporary table X); $ Db- & gt; exec ("Remove from B where in B (choose one from x)"); $ Db- & gt; exec ("Remove from A where one (choose from x)"); $ Db- & gt; Exec ("Select from B" (B) "); $ db = null; Edit:

    If you need all, then from B There are no records that are not present in A, you can do it like a simple SQL:

      Select b from where b is not (select A from A)    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -