gpt4 book ai didi

sql - 使用连接表选择属于另外两个的行

转载 作者:行者123 更新时间:2023-11-29 12:45:59 26 4
gpt4 key购买 nike

<分区>

给定两个模型 PlayerTeam 我如何选择两个球员特定球员所属的所有球队?

示例 Rails 模型和数据库表设置:

Rails 模型:

class Player < ActiveRecord::Base
has_and_belongs_to_many :teams, uniq: true
end
class Team < ActiveRecord::Base
has_and_belongs_to_many :players, uniq: true
end

SQL 表

    teams         players            players_teams
+----------------------------------------------------+
| id | name | | id | name | | player_id | team_id |
+----+------+ +----+------+ +-----------+---------+
| 1 | Foo | | 1 | Jack | | 1 | 1 |
| 2 | Bar | | 2 | Jill | | 1 | 2 |
| | | | | | | 2 | 1 |

我如何选择同时有 Jack 和 Jill 的所有团队? (在这种情况下,只有 Foo 团队。)

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com