标题: mysql查询 [打印本页] 作者: yongqiang126 时间: 2010-7-28 13:58 标题: mysql查询 两张表
表一机构为
user_id user_nickname
1 A
2 B
3 c
4 D
表二
mk_from mk_to
1 2
3 2
2 4
其实表二张的mk_from与mk_to都是表一中的user_id
现在想用一条语句查询出下面的结果
1 A
3 C
4 D
就是说表二的mk_to字段中有2这个值我查询其mk_from 1和3,表二的mk_from有2我查询其mk_to为4,然后组成结果就是1,3,4了。然后与表一再进行连接查询作者: je_ck 时间: 2010-7-28 15:01
写的不好,
SELECT t1_1.mk_from, Table_1.user_nickname
FROM (SELECT Table_2.mk_from
FROM Table_2 INNER JOIN
Table_2 AS t1 ON t1.mk_from = Table_2.mk_to
UNION
SELECT mk_to
FROM Table_2 AS Table_2_2
WHERE (mk_to NOT IN
(SELECT mk_from
FROM Table_2 AS Table_2_1))) AS t1_1 INNER JOIN
Table_1 ON t1_1.mk_from = Table_1.user_id作者: yongqiang126 时间: 2010-7-29 10:03 回复 2#je_ck