MySQL 中的两个左连接和一个并集
我正在尝试在 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'
。
我哪里出错了,我应该如何执行这个查询?
I'm trying to do a pretty complex query in MySQL
; complex for me, at least.
Here's an example of what I'm trying to do:
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;
I'm getting back: ERROR 1066 (42000): Not unique table/alias: 'users'
.
Where am I going wrong, and how should I really be performing this query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为您的表命名,例如:
Alias your table, like:
您已经使用表的不同字段编写了两次左连接,似乎当它被解析时,给它们别名,然后将它们与
friends
表连接You have written left join two times with different fields of tables, it seems to When it got parse so give them alias and then join them with
friends
Table