如何使多个If语句工作?

时间:2015-10-08 05:02:51

标签: mysql

我的语法只适用于单个IF语句,然后我决定检查ISBN是否已经存在,如果确实存在,则只返回“已存在”

我正在使用“功能”

这是我已经尝试过的。

IF EXISTS(SELECT ISBN FROM book WHERE ISBN=i) THEN
RETURN "Book already exists.";

ELSE
IF (b=0)
THEN
INSERT INTO book(ISBN,title,author,publisher,Date_Published,`Cost`)        VALUES(i,t,a,p,STR_TO_DATE(d, '%M %e,%Y'),c);
RETURN "Book has been successfully added.";
ELSE
UPDATE book SET ISBN=i, title=t,author=a,publisher=p,Date_Published=d,cost=c     WHERE bookID=b;
RETURN "Book has been successfully altered.";
END IF;

1 个答案:

答案 0 :(得分:1)

试试这样:

IF EXISTS(SELECT ISBN FROM book WHERE ISBN=i) THEN
  RETURN "Book already exists."; 
ELSEIF (b=0) THEN
   INSERT INTO book(ISBN,title,author,publisher,Date_Published,`Cost`)
   VALUES(i,t,a,p,STR_TO_DATE(d, '%M %e,%Y'),c);
   RETURN "Book has been successfully added.";
ELSE
   UPDATE book SET ISBN=i,  
   title=t,author=a,publisher=p,Date_Published=d,cost=c     
   WHERE bookID=b;
   RETURN "Book has been successfully altered.";
END IF;

您需要删除ELSE IF之间的空格