0

非常基本的sql问题。我不想将以下查询的输出显示为一个表而不是两个:

select players.name
from players
inner join teams
on players.team = teams.name
where teams.staysat = 'Ambassador'
and teams.checkin is not null;

select fans.name
from fans
where fans.staysat = 'Ambassador'
and teams.checkin is not null;

如何重写此文件,以便将球员姓名和球迷姓名打印为一个连续列表?

4

4 回答 4

8

你只需要在它们之间建立一个联合,就这么简单

select players.name
from players
inner join teams
on players.team = teams.name
where teams.staysat = 'Ambassador'
and teams.checkin is not null;
Union
select fans.name
from fans
where fans.staysat = 'Ambassador'
and teams.checkin is not null;

如果你不想省略重复的名字,你应该把Union All

于 2013-05-06T08:02:38.817 回答
2

尝试使用union all

select players.name
from players
inner join teams
on players.team = teams.name
where teams.staysat = 'Ambassador'
and teams.checkin is not null
union all
select fans.name
from fans
where fans.staysat = 'Ambassador'
and teams.checkin is not null;
于 2013-05-06T08:03:16.590 回答
2

使用union语句

select players.name as name
from players
inner join teams
on players.team = teams.name
where teams.staysat = 'Ambassador'
and teams.checkin is not null;
UNION (ALL)
select fans.name as name
from fans
where fans.staysat = 'Ambassador'
and teams.checkin is not null;
于 2013-05-06T08:03:23.787 回答
1

您可以Union用于Distinct值和Union All所有值(包括Duplicates

联合和联合所有

select players.name as Name
from players
inner join teams
on players.team = teams.name
where teams.staysat = 'Ambassador'
and teams.checkin is not null;
union(all)
select fans.name as Name
from fans
where fans.staysat = 'Ambassador'
and teams.checkin is not null;
于 2013-05-06T08:10:54.530 回答