2013-01-07 120 views
-2

是否可以在Select查询中执行此操作?MYSQL 5.x选择案例

select case customField4 
    case '4' 
     customField4 = 'bob' 
    case '5' 
     customField4 = 'bill' 
    case '6' 
     customField4 = 'terry' 
    case '7' 
     customField4 = 'bobby' 
    case '8' 
     customField4 = 'sue' 

但在选择查询?我见过If Else,但只能给我2个选项。我需要的不仅仅是这些。

帮助会很棒!

回答

2

当然,使用CASE表达:

SELECT CASE customField4 
    WHEN '4' THEN 'has 4' 
    WHEN '5' THEN 'has 5' 
    WHEN '6' THEN 'has 6' 
END AS customField4 
+0

那就是我正在寻找的:o) – StealthRT

+0

哎呀....这是为MySQL而不是MSSQL。 – StealthRT

+0

这并没有改变任何东西; http://dev.mysql.com/doc/refman/5.0/en//case.html – LittleBobbyTables

2

它看起来像你的问题可以通过简单地写

SELECT 'has ' + customField4 AS customField4 

但更一般地说来实现的,是:

SELECT 
    CASE customField4 
    WHEN '4' THEN 'has 4' 
    WHEN '5' THEN 'has 5' 
    WHEN '6' ... 
    END 

编辑:第一个片段是不是因为这个问题的选项当然是编辑了。

+0

是的大卫,这就是为什么我编辑,以更好地了解我在找什么,而不仅仅是追加。 – StealthRT

0

SELECT语句应该是:

SELECT CASE customField4 
    WHEN '4' 
     THEN 'has 4' 
    WHEN '5' 
     THEN 'has 5' 
    WHEN '6' 
     THEN 'has 6' 
    WHEN '7' 
     THEN 'has 7' 
    WHEN '8' 
     THEN 'has 8' 
    END 

SELECT关键字不是CASE的一部分。