Compare records between two tables in MYSQL

Asked

Viewed 3,324 times

0

I need a solution to compare any changes in the table products_csv in relation to table products even if it is only the exchange of a (letter or number) in some field.

I am using the code below but only works for unique records because if there are duplicate records in one 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)";

Example of Registry Change:

TABLE products_csv arrived with field produto_descricao='Bolas Azuis' and in the TABLE products is with the product field'

1 answer

2


Do the two tables have anything in common to reference each other? for example a PK? Then just make a join and compare fields. could be something like.

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.

  • Yeah, had PK worked out thanks !

  • 1

    Good!! for nothing! =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.