如何将此SQL查询显示为一个表

时间:2013-05-06 08:01:31

标签: sql

非常基本的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 个答案:

答案 0 :(得分: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;

如果您不想省略重复的名称,则应添加全部联盟

答案 1 :(得分: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;

答案 2 :(得分: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;

答案 3 :(得分:1)

您可以将Union用于不同值,将Union All用于所有值(包括重复项

<强> Union and Union All

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;