Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two table and find matching columns

Tags:

mysql

I have two tables table1 and table2, i need to write a select query which will list me the columns that exist in both the tables. (mysql)

I need to do for different tables (2 at a time)

Is this possible?

I tried using INFORMATION_SCHEMA.COLUMNS but am not able to get it right.

like image 823
Sharpeye500 Avatar asked Jun 17 '10 22:06

Sharpeye500


People also ask

How can I get matching records from two tables?

(INNER) JOIN : Returns records that have matching values in both tables. LEFT (OUTER) JOIN : Returns all records from the left table, and the matched records from the right table. RIGHT (OUTER) JOIN : Returns all records from the right table, and the matched records from the left table.

How do I compare two tables in Excel for matches?

Open the workbooks you want to compare. Go to the View tab, Window group, and click the View Side by Side button. That's it!

How do I compare two columns in Excel for matching values?

Navigate to the "Home" option and select duplicate values in the toolbar. Next, navigate to Conditional Formatting in Excel Option. A new window will appear on the screen with options to select "Duplicate" and "Unique" values. You can compare the two columns with matching values or unique values.

How do you compare data between two tables?

Compare two tables by using joins. To compare two tables by using joins, you create a select query that includes both tables. If there is not already an existing relationship between the tables on the fields that contain the corresponding data, you create a join on the fields that you want to examine for matches.


1 Answers

 SELECT a.COLUMN_NAME
 FROM INFORMATION_SCHEMA.COLUMNS a
 JOIN INFORMATION_SCHEMA.COLUMNS b
 ON a.COLUMN_NAME = b.COLUMN_NAME
 AND b.TABLE_NAME = 'table2'
 AND b.TABLE_SCHEMA = database() //or manually enter it
 WHERE a.TABLE_NAME = 'table1'
 AND a.TABLE_SCHEMA = database(); //or manually enter it
like image 116
Wrikken Avatar answered Sep 28 '22 08:09

Wrikken