How to use if statement in mysql to add text to a string value
I want to check if schoolid is 1, then add hyphen next year in two digit form to record.year
For example, if record.year = 1997, and schools.schoolid =1, then I would want record.year to return 1997-98.
$sqlquery = 'SELECT
record.year,
schools.school,
schools.schoolid,
sports.sport,
conferences.conference,
record.year
FROM
`record`,
`schoo开发者_StackOverflow中文版ls`,
`conferences`,
`sports`
WHERE
schools.schoolid = record.schoolid
AND
sports.sportid = record.sportid
AND
record.conferenceid = conferences.conferenceid';
Assuming that record.year
is of type INT
,
SELECT
IF
(
schools.schoolid = 1,
CONCAT(record.year,'-',RIGHT(record.year+1,2)),
record.year
) AS year,
schools.school,
....
if record.year is varchar
just cast it to number when adding hyphen:
SELECT
IF
(
schools.schoolid = 1,
CONCAT(record.year,'-',RIGHT(CAST(record.year AS SIGNED)+1,2)),
record.year
) AS year,
schools.school,
....
精彩评论