无法将 IF/ELSE in MySQL 与 PRINT 语句一起使用
Cant use IF/ELSE in MySQL with PRINT statement
我正在尝试在 MySQL 中的条件为真时打印一些内容。
这是我的代码:
BEGIN
UPDATE president
INNER JOIN election ON president.pres_name = election.candidate
SET yrs_serv = yrs_serv + 4
WHERE election.winner_loser_indic = 'W'
AND election.election_year = (SELECT MAX(election_year) FROM election)
BEGIN
IF (yrs_serv > 4) THEN BEGIN PRINT "warning"
END
END
END
当我 运行 出现这个错误时。
MySQL: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BEGIN IF (yrs_serv > 4) THEN BEGIN PRINT "warning" END END END' at line 7
这是一个过程。我想不通。如果我不尝试使用 IF
语句,代码就可以工作。
首先,update
末尾缺少 ;
,然后 IF
不需要 Begin
。然后 IF
的结尾由 END IF;
完成 只需要一个 Begin
.... End
试试下面的代码
BEGIN
UPDATE president
INNER JOIN election ON president.pres_name = election.candidate
SET yrs_serv = yrs_serv + 4
WHERE election.winner_loser_indic = 'W'
AND election.election_year = (SELECT MAX(election_year) FROM election);
IF (yrs_serv > 4) THEN
Select "warning"; // PRINT "warning" can be achived by Select
END IF;
END;
我正在尝试在 MySQL 中的条件为真时打印一些内容。 这是我的代码:
BEGIN
UPDATE president
INNER JOIN election ON president.pres_name = election.candidate
SET yrs_serv = yrs_serv + 4
WHERE election.winner_loser_indic = 'W'
AND election.election_year = (SELECT MAX(election_year) FROM election)
BEGIN
IF (yrs_serv > 4) THEN BEGIN PRINT "warning"
END
END
END
当我 运行 出现这个错误时。
MySQL: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BEGIN IF (yrs_serv > 4) THEN BEGIN PRINT "warning" END END END' at line 7
这是一个过程。我想不通。如果我不尝试使用 IF
语句,代码就可以工作。
首先,update
末尾缺少 ;
,然后 IF
不需要 Begin
。然后 IF
的结尾由 END IF;
完成 只需要一个 Begin
.... End
试试下面的代码
BEGIN
UPDATE president
INNER JOIN election ON president.pres_name = election.candidate
SET yrs_serv = yrs_serv + 4
WHERE election.winner_loser_indic = 'W'
AND election.election_year = (SELECT MAX(election_year) FROM election);
IF (yrs_serv > 4) THEN
Select "warning"; // PRINT "warning" can be achived by Select
END IF;
END;