我需要从一个特定的coach
开始职业生涯的year
中提取一个特定的team.id
,以下是数据示例:
比赛季节
| id | competition_id | season_id | name
22 48 14214 2017/2018
教练生涯
| coach_id | team_id | start | end
223496 69 2018-07-01 NULL
223496 4345 2011-10-01 2015-06-01
223496 15376 2011-02-01 2011-10-01
在这种情况下,我需要提取
coach
(2017/2018)中包含的year
2018年开始的season.name
职业,即team.id
=69。为此,我尝试了以下查询:
SELECT *
FROM coach_career cr
INNER JOIN coach c ON c.id = cr.coach_id
INNER JOIN competition_seasons s ON s.id = 22
WHERE cr.team_id = 69
AND `start` LIKE concat('%', cast(extract(year from s.name) as char(100)), '%')
但这会返回一个空的结果,我想我犯了一些错误,年提取,有人知道如何实现这一点?
注:表
coach
是coach_career
的主表,只包含此人的信息。 最佳答案
您可以尝试此查询。
因为你想把colnum和like
一起使用,所以用||
代替concat
。
然后将开始
LIKE '%' || s.name || '%'
条件移动到ON
的JOIN
中,因为INNER JOIN competition_seasons s ON s.id = 22
将返回所有播放。
SELECT *
FROM coach_career cr
INNER JOIN coach c ON c.id = cr.coach_id
INNER JOIN competition_seasons s ON `start` LIKE '%' || s.name || '%'
WHERE
s.id = 22
and
cr.team_id = 69
sqlfiddle
[结果]:
| coach_id | team_id | start | end | id | competition_id | season_id | name |
|----------|---------|------------|--------|----|----------------|-----------|-----------|
| 223496 | 69 | 2018-07-01 | (null) | 22 | 48 | 14214 | 2017/2018 |