MySQL子查询变量传递
表:用户(id INT,名字 VARCHAR)
表:阻止列表(id INT,userid INT,blocked_userid INT)
SELECT user.id theID, user.firstname
FROM user
WHERE
NOT user.id = 1000
AND IF ((SELECT id FROM blocklist WHERE theID = ANY (SELECT blocked_userid FROM blocklist WHERE userid = 1000)), 0, (IF ((1000 = ANY (SELECT userid FROM blocklist WHERE blocked_userid = theID)),0,1)))
我想返回所有用户的列表,而不是当前用户,比如说...用户 1000,未被阻止该用户或不在其他用户的阻止列表中的用户。因此,这是一个双向系统……您看不到您阻止的人,也可以看到阻止您的人。问题是,我不能很好地在子查询中使用“theID”。解决这个问题的最佳方法是什么?
table: User (id INT, firstname VARCHAR)
table: Blocklist (id INT, userid INT, blocked_userid INT)
SELECT user.id theID, user.firstname
FROM user
WHERE
NOT user.id = 1000
AND IF ((SELECT id FROM blocklist WHERE theID = ANY (SELECT blocked_userid FROM blocklist WHERE userid = 1000)), 0, (IF ((1000 = ANY (SELECT userid FROM blocklist WHERE blocked_userid = theID)),0,1)))
I want to return a list of all users not the current user, say... user 1000, that isn't blocked by that user or that isn't on the blocklist of another user. Therefor it's a two-way system... you can't see people you blocked and you can see people who blocked you. Problem is, I can't very well user "theID" in the subquery. What would be the best way to tackle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,根据示例数据,我认为这样做可以:
如果您确实需要删除两个额外的列 b1.id 和 b2.id:
Ok, based on the sample data I think this does it:
If you really need to get rid of the two extra columns b1.id and b2.id: