Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Match column values on the basis of the other two column values in MySQL
Let us first create a table −
mysql> create table DemoTable774 ( Id int, FirstName varchar(100), MatchId int ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable774 values(101,'Chris',104); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable774 values(102,'Adam',103); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable774 values(103,'Carol',102); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable774 values(104,'Bob',101); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable774;
This will produce the following output -
+------+-----------+---------+ | Id | FirstName | MatchId | +------+-----------+---------+ | 101 | Chris | 104 | | 102 | Adam | 103 | | 103 | Carol | 102 | | 104 | Bob | 101 | +------+-----------+---------+ 4 rows in set (0.00 sec)
Following is the query to display FirstName on the basis of ID and MatchID −
mysql> select tbl1.FirstName, tbl2.FirstName from DemoTable774 AS tbl1 left join DemoTable774 AS tbl2 ON( tbl1.MatchId = tbl2.Id );
This will produce the following output -
+-----------+-----------+ | FirstName | FirstName | +-----------+-----------+ | Bob | Chris | | Carol | Adam | | Adam | Carol | | Chris | Bob | +-----------+-----------+ 4 rows in set (0.03 sec)
Advertisements
