4

我正在尝试做一个非常复杂的查询 MySQL;至少对我来说很复杂。

这是我正在尝试做的一个例子:

SELECT * FROM friends
LEFT JOIN users ON users.uid = friends.fid1
LEFT JOIN users ON users.uid = friends.fid2
WHERE (friends.fid1 = 1) AND (friends.fid2 > 1)
UNION SELECT fid2 FROM friends
WHERE (friends.fid2  = 1) AND (friends.fid1 < 1)
ORDER BY RAND()
LIMIT 6;

我回来了:ERROR 1066 (42000): Not unique table/alias: 'users'

我哪里出错了,我应该如何真正执行这个查询?

4

2 回答 2

6

为您的表命名,例如:

LEFT JOIN users u1 ON u1.uid = friends.fid1
LEFT JOIN users u2 ON u2.uid = friends.fid2
于 2010-09-07T03:51:57.647 回答
2

您已经用表的不同字段编写了两次左连接,似乎当它被解析时,所以给它们别名,然后将它们与friends表连接

LEFT JOIN users users1 ON users1.uid = friends.fid1
LEFT JOIN users users2 ON users2.uid = friends.fid2
于 2010-09-07T03:57:39.507 回答