I want to learn how to compare two tables in order to find unmatched records.
I´m a total beginner to MySQL and therefore struggling currently in order to find out which query to use for comparing two tables.
Suppose, I have two tables that are identical in structure.
Table 1 (Old)
+------+--------+---------------+--------+-----------+
| "id" | "name" | "price" | "description" | "country" |
+------+--------+---------------+--------+-----------+
| "1" | "a" | "2" | "Lord " | "US" |
| "2" | "b" | "3" | "Taker" | "UK" |
+------+--------+---------------+--------+-----------+
Table 2 (New)
+------+--------+---------------+--------+-----------+
| "id" | "name" | "price" | "description" | "country" |
+------+--------+---------------+--------+-----------+
| "1" | "a" | "5" | "Lord" | "DE" |
| "2" | "b" | "6" | "Taker" | "JP" |
+------+--------+---------------+--------+-----------+
Table1 holds the outdated data and table2 the newest one.
I want to check if name, price, description fields from table 2 have any match to name, price, description fields in table 1. If not, it should update table 1 with data from table 2.
I checked out the folling site in order to find out to use which Mysql query: http://www.mysqltutorial.org/compare-two-tables-to-find-unmatched-records-mysql.aspx
But I cannot figure out how to write it down in Python.
I am new to using SQL statements in python and I have attempted it as,
import sys, mysql.connector
import mysql
try:
connection = mysql.connector.connect\
(host = "localhost", user = "root", passwd ="", db = "local")
except:
print("No connection")
sys.exit(0)
cursor = connection.cursor()
cursor.execute("SELECT name, price, description FROM table2_new WHERE
I´m do not really have a clue how to continue. I searched for similar tasks but do not fully understand how to do it in Python.
Thats the desired output for table 1, the prices should be updated:
+------+--------+---------------+--------+-----------+
| "id" | "name" | "price" | "description" | "country" |
+------+--------+---------------+--------+-----------+
| "1" | "a" | "5" | "Lord " | "US" |
| "2" | "b" | "6" | "Taker" | "UK" |
+------+--------+---------------+--------+-----------+
Could you guys help me out? Any feedback is appreciated.