这是所谓的“自连接”查询,如下为示例:mysql> select a.* from a, a as b where a.uid = 1 and a.uid = b.sid and a.sid = b.uid; +-----+-----+ | uid | sid | +-----+-----+ | 1 | 2 | | 1 | 3 | +-----+-----+ 2 rows in setmysql>
表中没有唯一标识的字段,加入自增字段ID select a.* from ttr a, ttr b where a.id<b.id and a.uid = b.sid and a.sid = b.uid and a.uid=1;
select * from 表A t1 ,表A t2 where t1.uid=t2.sid and t1.sid=t2.uid wand t1.uid=1
from a, a as b
where a.uid = 1 and a.uid = b.sid and a.sid = b.uid;
+-----+-----+
| uid | sid |
+-----+-----+
| 1 | 2 |
| 1 | 3 |
+-----+-----+
2 rows in setmysql>
select a.*
from ttr a, ttr b
where a.id<b.id and a.uid = b.sid and a.sid = b.uid and a.uid=1;
where t1.uid=t2.sid
and t1.sid=t2.uid
wand t1.uid=1