这里似乎有很多冗余。有更有效的方法,还是需要使用工会?此外还有inner join
,因为players
表没有酒店信息,必须从teams
检索。
select players.name
from players
inner join teams
on players.team = teams.name
where teams.staysat = 'Ambassador'
and teams.checkin is not null
and teams.checkin < '18-AUG-07'
and teams.checkout > '18-AUG-07'
--
union all
select officials.name
from officials
where officials.staysat = 'Ambassador'
and officials.checkin is not null
and officials.checkin < '18-AUG-07'
and officials.checkout > '18-AUG-07'
--
union all
select fans.name
from fans
where fans.staysat = 'Ambassador'
and fans.checkin is not null
and fans.checkin < '18-AUG-07'
and fans.checkout > '18-AUG-07';
答案 0 :(得分:3)
是的,因为你从不同的表(玩家,粉丝,官员)中检索数据,这里需要联合,我只能想象如果你提供一个从你的表中提取所需信息的功能,你可以缩短它。将指定为一个字符串,但这将导致使用串联或其他头痛的东西建立查询字符串,所以可能更好地坚持联合选择;)
此外,如果表中的其他列也没有差异,您可以考虑将它们全部放在一个表中,并通过标志或类似的方式区分它们。
答案 1 :(得分:2)
你有正确的方法去做。工会和内部联合是必要的。
答案 2 :(得分:0)
您可以使用子查询或查看(首选),如下所示:
select * from (
select players.name as name, teams.checkin as checkin, teams.checkout as checkount, teams.staysat as staysat
from players
inner join teams
on players.team = teams.name
union all
select officials.name as name, officials.checkin as checkin, officials.checkout as checkount, officials.staysat as staysat
from officials
union all
select fans.name as name, fans.checkin as checkin, fans.checkout as checkount, fans.staysat as staysat
from fans
) as t
where t.staysat = 'Ambassador'
and t.checkin is not null
and t.checkin < '18-AUG-07'
and t.checkout > '18-AUG-07';
向外移动条件,但可能更慢但更简单