Syed Nizamudeen
Syed Nizamudeen

Reputation: 440

MySQL Join Query { How to get following Result }

Table 1

 +----------+
 |id | data |
 +----------+
 |1  | USRA |
 +----------+
 |4  | USRB |
 +----------+

Table 2

+----------+
|cid | mid |
+----------+
|1  |  4   |
+----------+
|4  |  1   |
+----------+

Result Table

+----------------------+
|table1_id | table2_id |
+----------------------+
|USRA      | USRB      |
+----------------------+
|USRB      | USRA      |
+----------------------+

Upvotes: 0

Views: 43

Answers (3)

underscore
underscore

Reputation: 6887

SELECT t3.data, t1.data
FROM a t1, b t2, a t3
WHERE t1.id = t2.cid
AND   t3.id = t2.mid

enter image description here

Upvotes: 0

SMA
SMA

Reputation: 37023

Try:

SELECT t1.data, t3.data
FROM tab1 t1, tab2 t2, tab1 t3
WHERE t1.id = t2.id
AND   t3.id = t2.mid

Upvotes: 0

mlinth
mlinth

Reputation: 3108

SELECT
 a.data,
 b.data
FROM
 Table1 a
INNER JOIN
 Table2 t2
ON a.id = t2.cid
INNER JOIN
 Table1 b
ON b.id = t2.mid

Upvotes: 1

Related Questions