MariaDB 找到一个参加所有球队的足球运动员
MariaDB finding a soccer player that participated in all teams
我正在尝试在 MariaDB 中查询 returns 我在所有球队中出场的球员的名字。我是查询的新手,并且没有使用内部连接的运气(主要是因为不太了解它),并且使用 IN 的所有尝试都没有那么好,ideias?
编辑:我现在不在我的电脑前,所以我没有关于代码的具体示例,但它就像
SELECT Soccer.player
FROM Soccer
WHERE Soccer.player in (SELECT * FROM Teams, TeamPlayers
WHERE Teams.tid = TeamPlayers.tid);
你可以这样做:
示例数据
create table soccer (player varchar(100));
insert into soccer values ('john'), ('matt'), ('katie');
create table teams (teamname varchar(100));
insert into teams values ('A'), ('B'), ('C');
create table teamplayers (team varchar(100), player varchar(100));
insert into teamplayers values
('A', 'katie'), ('B', 'katie'), ('C', 'katie'),
('B', 'john'), ('C', 'john'),
('C', 'matt');
预期结果
由于 katie
是所有团队中唯一的玩家,我们应该打印她的名字。
查询更简单
select tp.player
from teamplayers tp
inner join teams t on t.teamname = tp.team
group by tp.player
having count(*) = (select count(*) from teams);
说明
- 加入团队成员和团队
- 分组播放器(并在
having
语句中找到计数)
- 如果计数与团队计数相匹配,select 该玩家
SQL Fiddle
http://sqlfiddle.com/#!9/7110b/15
查询
这个查询可以用不同的方式编写。我以一种希望对内联解释有意义的方式编写它
select player
from soccer s
where not exists (
select 1
from
-- get all possible combinations of team and players
(select player, teamname from soccer, teams) main
-- combine the above with team players
left join teamplayers tp
on tp.team = main.teamname
and tp.player = main.player
-- find those players who DO NOT end up in one or more teams
-- and exclude those from select (see where NOT EXISTS)
where tp.team is null
and main.player = s.player
);
说明
- 要知道一个人是否在所有团队中,让我们为所有团队中的每个球员制作一个矩阵(每个人都在所有团队中的场景)
- 将此场景与团队合作 table 进行比较。那些不在一个或多个团队中的玩家将在
tp.team
字段中有 NULL
- 将所有玩家与上面的列表进行比较,然后选择没有出现在该列表中的玩家
结果
凯蒂
SQLFiddle 例子
SELECT DISTINCT store_type FROM stores s1
WHERE NOT EXISTS (
SELECT * FROM cities WHERE NOT EXISTS (
SELECT * FROM cities_stores
WHERE cities_stores.city = cities.city
AND cities_stores.store_type = stores.store_type));
在 NOT EXISTS 解释网站上找到这个例子,效果很好!但是谢谢@zedfoxus,希望有一天我能如此轻松地做这些事情
我正在尝试在 MariaDB 中查询 returns 我在所有球队中出场的球员的名字。我是查询的新手,并且没有使用内部连接的运气(主要是因为不太了解它),并且使用 IN 的所有尝试都没有那么好,ideias?
编辑:我现在不在我的电脑前,所以我没有关于代码的具体示例,但它就像
SELECT Soccer.player
FROM Soccer
WHERE Soccer.player in (SELECT * FROM Teams, TeamPlayers
WHERE Teams.tid = TeamPlayers.tid);
你可以这样做:
示例数据
create table soccer (player varchar(100));
insert into soccer values ('john'), ('matt'), ('katie');
create table teams (teamname varchar(100));
insert into teams values ('A'), ('B'), ('C');
create table teamplayers (team varchar(100), player varchar(100));
insert into teamplayers values
('A', 'katie'), ('B', 'katie'), ('C', 'katie'),
('B', 'john'), ('C', 'john'),
('C', 'matt');
预期结果
由于 katie
是所有团队中唯一的玩家,我们应该打印她的名字。
查询更简单
select tp.player
from teamplayers tp
inner join teams t on t.teamname = tp.team
group by tp.player
having count(*) = (select count(*) from teams);
说明
- 加入团队成员和团队
- 分组播放器(并在
having
语句中找到计数) - 如果计数与团队计数相匹配,select 该玩家
SQL Fiddle
http://sqlfiddle.com/#!9/7110b/15
查询
这个查询可以用不同的方式编写。我以一种希望对内联解释有意义的方式编写它
select player
from soccer s
where not exists (
select 1
from
-- get all possible combinations of team and players
(select player, teamname from soccer, teams) main
-- combine the above with team players
left join teamplayers tp
on tp.team = main.teamname
and tp.player = main.player
-- find those players who DO NOT end up in one or more teams
-- and exclude those from select (see where NOT EXISTS)
where tp.team is null
and main.player = s.player
);
说明
- 要知道一个人是否在所有团队中,让我们为所有团队中的每个球员制作一个矩阵(每个人都在所有团队中的场景)
- 将此场景与团队合作 table 进行比较。那些不在一个或多个团队中的玩家将在
tp.team
字段中有 NULL - 将所有玩家与上面的列表进行比较,然后选择没有出现在该列表中的玩家
结果
凯蒂
SQLFiddle 例子
SELECT DISTINCT store_type FROM stores s1
WHERE NOT EXISTS (
SELECT * FROM cities WHERE NOT EXISTS (
SELECT * FROM cities_stores
WHERE cities_stores.city = cities.city
AND cities_stores.store_type = stores.store_type));
在 NOT EXISTS 解释网站上找到这个例子,效果很好!但是谢谢@zedfoxus,希望有一天我能如此轻松地做这些事情