Compare records between two tables in MYSQL

0

I need a solution that compares any changes you've made to the products_csv table against the products table ( letter or number ) in some field.

I'm using the code below but only works for single records because if there are duplicate records in a loop does not answer.

 $sql= "SELECT * FROM produtos_csv 
                WHERE produto_codigo NOT IN (SELECT produto_codigo FROM produtos)
                OR produto_descricao NOT IN (SELECT produto_descricao FROM produtos)";

Sample registry change:

products_csv TABLE arrived with field produto_descricao='Bolas Azuis' and in the TABLE products is with the field producto_descricao = 'Blue Ball'

    
asked by anonymous 22.03.2018 / 20:02

1 answer

1

Does the two tables have anything in common to refer to each other? for example a PK ? Then just make a join and compare the fields. could be something of the sort.

SELECT
    p1.*,
    p2.*
FROM produtos_csv p1
INNER JOIN produtos p2
ON p2.codigo = p1.codigo -- DIGAMOS QUE ISSO SEJA SUA PK
WHERE p1.produto_descricao != p2.produto_descricao
OR p1.produto_codigo_barra != p2.produto_codigo_barra
OR ... -- E vai adicionando as comparações necessárias

This will result in products that are different between them.

    
22.03.2018 / 20:41