gpt4 book ai didi

mysql - 无法将年份与不同的表字段进行比较

转载 作者:行者123 更新时间:2023-11-29 05:04:24 25 4
gpt4 key购买 nike

我需要为特定的 team.id 提取一个从特定的 year 开始职业生涯的 coach,这些是数据样本:

竞争季节

| 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

在本例中,我需要提取 season.name 中包含的 year 2018 年开始的 coach 职业生涯(2017/2018),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)), '%')

但这将返回一个空结果,我想我在提取年份时犯了一些错误,有人知道如何实现吗?

注意:表coachcoach_career的主表,只包含人员的信息。

最佳答案

您可以试试这个查询。

因为您希望将 colnum 与 like 一起使用,所以 || 而不是 concat

然后移动开始LIKE '%' ||名字 || '%' 条件为 JOINON,因为 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 |

关于mysql - 无法将年份与不同的表字段进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51396565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com