Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rails 3/SQL-复杂的“存在?”查询_Sql_Ruby On Rails 3_Sqlite - Fatal编程技术网

Rails 3/SQL-复杂的“存在?”查询

Rails 3/SQL-复杂的“存在?”查询,sql,ruby-on-rails-3,sqlite,Sql,Ruby On Rails 3,Sqlite,我的模型 class Team < ActiveRecord::Base has_many :team_players has_many :players, :through => :team_players end class TeamPlayer < ActiveRecord::Base belongs_to :team belongs_to :player end 我想你可以分两步来做: 找到拥有你所关注的所有球员的球队。 检查这些球队的球员是否比你看

我的模型

class Team < ActiveRecord::Base
  has_many :team_players
  has_many :players, :through => :team_players
end

class TeamPlayer < ActiveRecord::Base
  belongs_to :team
  belongs_to :player
end

我想你可以分两步来做:

找到拥有你所关注的所有球员的球队。 检查这些球队的球员是否比你看到的球员多。 也就是说:

select team_id
from team_players
where team_id in (
    select team_id
    from team_players
    where player_id in (1,3)
    group by team_id
    having count(player_id) = 2
)
group by team_id
having count(player_id) = 2
鉴于团队成员的这种情况:

上面的查询显示team_id=2,这就是您的精确匹配。您正在使用的查询将为您提供包含有问题的玩家作为子集的团队,而不是设置为与有问题的玩家相等的团队

如果您只想知道这样一个团队是否存在,那么将该查询包装在select_rows调用中,就完成了:

class Team < ActiveRecord::Base
    # players is an array of Fixnum player IDs
    def self.team_exist?(players)
        return false if(!players.present?)
        connection.select_rows(%Q{
            select team_id
            from team_players
            where team_id in (
                select team_id
                from team_players
                where player_id in (#{players.join(',')})
                group by team_id
                having count(player_id) = #{players.length}
            )
            group by team_id
            having count(player_id) = #{players.length}
        }).present?
    end
end

{players.join','}插值假设team_存在?正在向您提供一个Fixnum数组,因此您需要在调用者中正确清理数据,或添加一个players=players.map&:to\u i,然后再连接。选择\u rows调用以在团队内部清理数据\u exist?

谢谢!我没有使用connection.select_rows,因为我想获取实际的team_id值。但是查询本身工作得很好。除了你在上一次考试中忘了定义“c”part@Frexuz当前位置具有c。。。应该有countplayer\u id。。。正当我在翻译我编造的胡说八道的名字和你的真名。
team_id|player_id
     1 | 1
     1 | 3
     1 | 4
     2 | 1
     2 | 3
     3 | 3
class Team < ActiveRecord::Base
    # players is an array of Fixnum player IDs
    def self.team_exist?(players)
        return false if(!players.present?)
        connection.select_rows(%Q{
            select team_id
            from team_players
            where team_id in (
                select team_id
                from team_players
                where player_id in (#{players.join(',')})
                group by team_id
                having count(player_id) = #{players.length}
            )
            group by team_id
            having count(player_id) = #{players.length}
        }).present?
    end
end