【发布时间】:2021-05-02 04:03:27
【问题描述】:
这是我当前的查询。我今天发现了 INTERSECT,但是 MySQL 不支持它。谁能给我一些关于如何改进这个查询的建议?它为我提供了我正在寻找的结果,但速度相当缓慢。
如果有帮助,我正在与运动队合作,并过滤统计数据。我过滤主队,然后是客队。我使用了 count(id) 方法,因为我只想返回在 both 查询中找到的灯具 ID,所以我认为 INTERSECT 会很完美。
select id, count(id) as count from (
select f.id, f.etc, etc
from fixtures f
inner join team_stats t on f.id = t.fixture_id and f.league_id = t.league_id and f.home_id = t.team_id
where f.status in ('FT', 'AET', 'PEN', 'FT_PEN') and
unix < 1619927451 and
played_overall between '11' and '60' and
scored_1h_home_avg between '0.7' and '6' and
o05_1h_goals_overall_per between '76' and '100' and
first_goal_total_avg between '0' and '35' and
scored_o05_1h_home_per >= 69
UNION ALL
select f.id, f.etc, etc
from fixtures f
inner join team_stats t on f.id = t.fixture_id and f.league_id = t.league_id and f.away_id = t.team_id
where f.status in ('FT', 'AET', 'PEN', 'FT_PEN') and
unix < 1619927451 and
played_overall between '11' and '60' and
conceded_1h_away_avg between '0.7' and '6' and
first_goal_total_avg between '0' and '35' and
o05_1h_goals_away_per between '77' and '100'
)
x group by id HAVING count(id) > 1
【问题讨论】:
-
谨防将整数视为字符串;在某些情况下,结果可能不是您所期望的:db-fiddle.com/f/g7stKibxTZjW9ikroqCeX9/0