The difference between two tables in MySQL
In this article, we will learn how to find the difference between two tables in MySQL.
The difference between two tables in MySQL
We often need to compare two tables to find records in one table that have no matching records in the other table.
For example, our new database has a different schema than the old database. It is our responsibility to transfer all the data from the old database to the new database and ensure that the transfer is successful.
To validate the data, we have to compare two tables—one from the old database and one from the new database—and find any records that don't match.
Consider a situation where we need to compare the columns of two tables table_1 and table_2. The following steps compare the two tables to find the mismatching records.
First, use the UNION statement to join the rows from the two tables; include only the necessary columns. Use the returned result set to perform the comparison.
SELECT table_1.primary_key, table_1.column_1, table_1.column2
FROM table_1
UNION ALL
SELECT table_2.primary_key, table_2.column_1, table_2.column2
FROM table_2;
The records should then be grouped based on the primary key and the columns that need to be compared. If the values in the columns that need to be compared are similar, COUNT(*)
the function produces 2; otherwise, it returns 1.
SELECT primary_key, column_1, column_2
FROM
(
SELECT table_1.primary_key, table_1.column_1
FROM table_1
UNION ALL
SELECT table_2.primary_key, table_2.column_1
FROM table_2
) temporary_table
GROUP BY primary_key, column_1, column_2
HAVING COUNT(*) = 1
ORDER BY primary_key
Consider the following example to help you better understand the previous idea.
SELECT employee_id, department, email
FROM (
SELECT employee_id, department, email FROM employee_old_table
UNION ALL
SELECT employee_id,department, email FROM employee_new_table
) temporary_table
GROUP BY employee_id, department, email
HAVING count(*) = 1
ORDER BY employee_id;
In the above example, the old and new employee tables are compared and the results are stored in a temporary table. After the results are returned, we will group them by employee_id, department name and email id.
If the values in the columns that have to be compared are identical, COUNT(*)
the method returns 2; otherwise, it returns 1. Therefore, we use the HAVING clause to check for unique values.
Finally, we print the results in ascending order by employee_id.
Run the above line of code in any browser compatible with MySQL. It will display the following result:
+-------------+--------------------+----------------------+
| employee_id | department | email |
+-------------+--------------------+----------------------+
| 14 | TeleCom | john_doe@example.com |
| 15 | TeleCommunication | johndoe@example.com |
+-------------+--------------------+----------------------+
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Display tables and database structure in MySQL
Publish Date:2025/04/23 Views:97 Category:MySQL
-
Today, we will learn about queries in MySQL that can display the table and database structure. We will use the mysqldump utility, DESCRIBE the , SHOW TABLES and SHOW CREATE TABLE the statements. We are using MySQL version 8.0.28 while writi
Select first row from MySQL table
Publish Date:2025/04/23 Views:112 Category:MySQL
-
Today, we will explore three scenarios and their solutions where we want to select the first row from a MySQL table. In the first scenario, we will learn to get the first row from a MySQL table where there are multiple instances of a partic
Insert timestamp into MySQL table
Publish Date:2025/04/23 Views:77 Category:MySQL
-
Today, we will learn how to TIMESTAMP insert date and time into a type column of a MySQL table according to the table definition. Create a MySQL table First, we will create the tables that we will use in this tutorial. Sample code: CREATE T
Converting from datetime type to date-only in MySQL
Publish Date:2025/04/23 Views:199 Category:MySQL
-
Today, we will learn the DATE(), CAST(), CONVERT() and DATE_FORMAT() methods to convert DATETIME type to DATE type in MySQL. The above mentioned methods can be used in MySQL 4.0 and above. Converting from DATETIME to DATE in MySQL To unders
Changing max_allowed_packet Size in MySQL Server
Publish Date:2025/04/22 Views:193 Category:MySQL
-
This article explains how to change the max_allowed_packet size in MySQL server. To understand this, we will use two operating systems, Windows 10 and Linux (Ubuntu). Changing max_allowed_packet Size in MySQL Server If we try to upload a fi
Zerofill usage, advantages and alternatives in MySQL
Publish Date:2025/04/22 Views:196 Category:MySQL
-
In this article we will understand the uses, advantages and alternatives of ZEROFILL attribute in MySQL. Use and benefits of the ZEROFILL attribute in MySQL The benefit of using the ZEROFILL attribute is that it has nothing to do with input
Compare only MySQL timestamp dates to date parameters
Publish Date:2025/04/22 Views:64 Category:MySQL
-
In this article we will use the DATE() , CAST() , and CONVERT() functions to compare MySQL timestamp dates with only the date parameter. DATE() vs. CAST() vs. CONVERT() in MySQL Below is a brief description of each function. You can also fi
Calculating Percentages in MySQL
Publish Date:2025/04/22 Views:67 Category:MySQL
-
We will use one or more columns to calculate percentages in MySQL. There are different ways to do this, and for each method we will use an example table. Calculate percentage using a column in MySQL We have a table called sales where ID, Re
Selecting multiple values using WHERE in MySQL
Publish Date:2025/04/22 Views:186 Category:MySQL
-
This article is about using MySQL query to get data from a specific table or relation that satisfies a specific condition. To do this, the WHERE clause is used in the SQL query. WHERE clause in SQL query WHERE The clause specifies the condi