gpt4 book ai didi

mysql - 如何在mysql中使用if语句将文本添加到字符串值

转载 作者:行者123 更新时间:2023-11-29 07:08:31 25 4
gpt4 key购买 nike

我想检查schoolid是否为1,然后在record.year中以两位数的形式添加下一年的连字符

例如,如果 record.year = 1997,且 schools.schoolid =1,那么我希望 record.year 返回 1997-98。

$sqlquery = 'SELECT 
record.year,
schools.school,
schools.schoolid,
sports.sport,
conferences.conference,
record.year
FROM
`record`,
`schools`,
`conferences`,
`sports`
WHERE
schools.schoolid = record.schoolid
AND
sports.sportid = record.sportid
AND
record.conferenceid = conferences.conferenceid';

最佳答案

假设record.yearINT类型,

SELECT    
IF
(
schools.schoolid = 1,
CONCAT(record.year,'-',RIGHT(record.year+1,2)),
record.year
) AS year,
schools.school,
....

如果 record.year 是 varchar 在添加连字符时将其转换为数字:

SELECT    
IF
(
schools.schoolid = 1,
CONCAT(record.year,'-',RIGHT(CAST(record.year AS SIGNED)+1,2)),
record.year
) AS year,
schools.school,
....

关于mysql - 如何在mysql中使用if语句将文本添加到字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5922677/

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