mysql 仅当另一行的字段等于时才选择一行
我有这两个表:
tAccounts
id、名称、server_id
tFriends
id_lo, id_hi
现在,我正在尝试这个查询:
SELECT FR.id_lo AS id_friend
FROM tFriends FR, tAccounts AC
WHERE (FR.id_hi = 4 && AC.server_id != -1)
........ 为了获取某个用户的好友,同时确保他的好友的 server_id 不同于“-1” !
有什么想法吗? WHERE 子句中的 'AC.server_id != -1' 约束似乎没有给我预期的结果。
I have these two tables:
tAccounts
id, name, server_id
tFriends
id_lo, id_hi
Now, I'm trying this query:
SELECT FR.id_lo AS id_friend
FROM tFriends FR, tAccounts AC
WHERE (FR.id_hi = 4 && AC.server_id != -1)
........ In order get the friends of a certain user, while making sure his friend's server_id is different than '-1' !
Any idea? It seems like the 'AC.server_id != -1' constraint in the WHERE clause doesn't give me the expected results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的加盟条件在哪里?应该是这样的:(我假设tFriends.id_lo是朋友的帐户id)
Where is you join condition ? It should be something like this: (I assume that tFriends.id_lo is an account id of friend)
您需要连接两个表;否则,您将得到笛卡尔积(将 tFriends 中 id_hi = 4 的所有行与 tAccounts 中 server_id 不为 -1 的所有行组合起来)。
试试这个:
You need to join the two tables; otherwise, you get a Cartesian product (combining all rows of tFriends where id_hi = 4 with all rows of tAccounts where server_id is not -1).
Try this:
尝试连接帐户表两次:
或使用 UNION:
Try joining the account table twice:
Or use a UNION: