Rails SQL INTERSECT无法正常工作

时间:2014-04-19 22:18:06

标签: mysql sql ruby-on-rails

我希望得到一个用户的共同朋友,即我关注的人并跟着我回来:

# 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。* FROM users WHERE(id   IN(SELECT followed_id FROM relationships WHERE follower_id = 2   INTERSECT SELECT follower_id FROM relationships WHERE followed_id =   2))

单个查询确实有效,所以我知道INTERSECT关键字存在问题。

1 个答案:

答案 0 :(得分:0)

MySQL没有INTERSECT。有关详细信息,请参阅hereherehere