CASE声明效果不佳

时间:2014-11-19 13:11:30

标签: mysql

嘿伙计们,我实际上是mysql的新手......我写了类似

的代码
 CREATE TABLE CUSTOMERS(
   ID   INT              NOT NULL,
   NAME VARCHAR (20)     NOT NULL,
   AGE  INT              NOT NULL,
   ADDRESS  CHAR (25) ,
   SALARY   DECIMAL (18, 2), 
   PRIMARY KEY (ID)
);


INSERT INTO  CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)  
VALUES (1,'aff',2,3,5);

我的案例陈述看起来像

 select CASE WHEN ID > 0 THEN SET NAME  = 'SDFSDF'; ELSE SET NAME = 'ASD'
END CASE;
from CUSTOMERS

当我在sql小提琴上运行它时,它给出了像

这样的错误

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 'SET NAME = 'SDFSDF'; ELSE SET NAME = 'ASD' END CASE' at line 1: select CASE WHEN ID > 0 THEN SET NAME = 'SDFSDF'; ELSE SET NAME = 'ASD' END CASE

任何人都可以帮助我..任何帮助都会受到赞赏

6 个答案:

答案 0 :(得分:2)

正确的陈述是

select 
case 
 when id > 0 then 'sdfsdf' 
 else 'asd' end as name 
from customers ;

答案 1 :(得分:2)

您的实施是错误的。您的查询结构应该是这样的

SELECT CASE WHEN ID > 0 
                THEN 'SDFSDF' 
                ELSE 'ASD'
           END AS NAME
    FROM CUSTOMERS

答案 2 :(得分:2)

您是否尝试更新表格中的值?

在这种情况下,请使用:

UPDATE customers SET name = CASE WHEN ID > 0 THEN 'SDFSDF' ELSE 'ASD' END

答案 3 :(得分:1)

为什么不执行错误消息建议? Read about the right syntax。 您应该注意到您的陈述中;不属于那里。

正确的陈述应为:

select CASE WHEN ID > 0 
           THEN 'SDFSDF'
           ELSE 'ASD'
       END CASE as Name
from CUSTOMERS;

请注意,;THEN之后没有END。此外,SET也不属于那里。

答案 4 :(得分:1)

使用此

select CASE WHEN ID > 0 THEN SET NAME  = 'SDFSDF' ELSE SET NAME = 'ASD'
END CASE as customer_name from CUSTOMERS

答案 5 :(得分:0)

盖伊 你的问题很糟糕。你没有做任何努力。只需查看CASE control function

的语法

您应该从最后删除CASE字。

Select CASE WHEN ID > 0 THEN SET NAME  = 'SDFSDF'; ELSE SET NAME = 'ASD'
END /* CASE */;
/*from CUSTOMERS*/

答案:

select *, (Select CASE WHEN ID > 0 THEN SET NAME  = 'SDFSDF'
          ELSE SET NAME = 'ASD' END)
from CUSTOMERS