我希望得到一个用户的共同朋友,即我关注的人并跟着我回来:
# get users I'm following
following_ids = "SELECT followed_id FROM relationships WHERE follower_id = #{self.id}"
# get user that are following me
follower_ids = "SELECT follower_id FROM relationships WHERE followed_id = #{self.id}"
User.where("id IN (#{following_ids} INTERSECT #{follower_ids})")
但是,我收到以下错误:
Mysql2 ::错误:您的SQL语法中有错误;检查手册 对应于您的MySQL服务器版本,以获得正确的语法 使用near' INTERSECT SELECT follower_id FROM relationships WHERE followed_id = 2))'第1行:SELECT
users
。* FROMusers
WHERE(id IN(SELECT followed_id FROM relationships WHERE follower_id = 2 INTERSECT SELECT follower_id FROM relationships WHERE followed_id = 2))
单个查询确实有效,所以我知道INTERSECT关键字存在问题。