Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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
Sql 从多值属性中选择元组_Sql_Sqlite_Multivalue - Fatal编程技术网

Sql 从多值属性中选择元组

Sql 从多值属性中选择元组,sql,sqlite,multivalue,Sql,Sqlite,Multivalue,我正在尝试为下面的示例获取一个查询,其中我需要选择所有为同一个团队或同一个团队打球的人,以及其他一些人。比如说 person teamName 1 maple leafs 1 rangers 2 red wings 3 rangers 3 avalanche 3 maple leafs 我正在尝试创建一个查询,该查询表示查找所有与玩家1在同一球队踢球的玩家。在上面的例子中,它应该返回玩家1和玩家3。我有些事要做 sele

我正在尝试为下面的示例获取一个查询,其中我需要选择所有为同一个团队或同一个团队打球的人,以及其他一些人。比如说

person  teamName
1       maple leafs
1       rangers
2       red wings
3       rangers
3       avalanche
3       maple leafs
我正在尝试创建一个查询,该查询表示查找所有与玩家1在同一球队踢球的玩家。在上面的例子中,它应该返回玩家1和玩家3。我有些事要做

select person from teams where teamName = 'maple leafs' intersect select person from teams where teamName = 'rangers';
然而,这是太硬编码的球员可能会被送往另一个团队。我可以得到一个球员名单谁已经在一队的球员1与以下

create table temp as select teamName from teams where person = 1;
select * from temp join teams on temp.teamName = teams.teamName;
但我不知道如何提取与第一名球员在同一队的球员。我曾经尝试过分组方式和使用子句,但是当我分组时,任何超过第一个的团队都会失败,所以我有点卡住了


非常感谢您的帮助。

您可以这样概括查询:

select person
from yourtablename
where teamname in (select teamname from yourtablename where person = 1)
group by person
having count(*) = (select count(*) from yourtablename where person = 1);

第一个子选择将团队限制为仅限于person 1所在的团队。但这仍然包括至少参加过一个团队但不是所有团队的人。然后HAVING子句确保查询返回的人员具有与person 1相同的团队数量,这意味着他们已经加入了所有相同的团队。

太好了,是的,我最后使用count*比较了两个表,谢谢!