MySQL加入查询{如何获得以下结果}

时间:2014-11-06 08:15:33

标签: mysql join

表1

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

表2

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

结果表

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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

尝试:

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

答案 2 :(得分:0)

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