2017-07-23 69 views
0

就像這樣:我可以在MySQL中的CASE ... END子句中的一個WHEN中放置兩個(或更多)條件嗎?

SET v_tax_rate = 
    CASE state 
     WHEN 'AL', 'AK' THEN 0.04 
     WHEN 'CA' THEN 0.06 
     WHEN 'WY' THEN 0.03 
    END; 

我把 'AL' 和 'AK' 在一個WHEN。但它不起作用。我無法成功創建它,並收到錯誤提示。

在MySQL中可以這樣做嗎?

+1

你試過了嗎?當'AL'和'AK'然後是0.04'?我沒有MySQL,但它在Postgres中有效。 –

+0

謝謝!我嘗試了你的建議,但它不起作用。替換AND與OR導致相同。 – Benson

+1

哦,是的,當然你會想'OR'在那裏。如果你做'CASE當'狀態='AL'或狀態='AK'然後0.04'。這就是我在Postgres中真正做到的。 –

回答

1

根據目前12.4 Control Flow Functions :: CASE語法,理解和警告的正確處理,一個選擇是像做:

mysql> DROP TABLE IF EXISTS `tbl_sta`; 
Query OK, 0 rows affected (0.00 sec) 

mysql> CREATE TABLE IF NOT EXISTS `tbl_sta` (
    -> `state` CHAR(2) NOT NULL 
    ->); 
Query OK, 0 rows affected (0.00 sec) 

mysql> INSERT INTO `tbl_sta` 
    -> (`state`) 
    -> VALUES 
    -> ('AL'), ('AK'), 
    -> ('CA'), ('WY'); 
Query OK, 4 rows affected (0.00 sec) 
Records: 4 Duplicates: 0 Warnings: 0 

mysql> SELECT 
    -> `state`, 
    -> CASE `state` 
    ->  WHEN `state` NOT IN ('AL', 'AK') THEN 0.04 
    ->  WHEN 'CA' THEN 0.06 
    ->  WHEN 'WY' THEN 0.03 
    -> END `result` 
    -> FROM `tbl_sta`; 
+-------+--------+ 
| state | result | 
+-------+--------+ 
| AL | 0.04 | 
| AK | 0.04 | 
| CA | 0.06 | 
| WY | 0.03 | 
+-------+--------+ 
4 rows in set, 4 warnings (0.01 sec) 

mysql> SHOW WARNINGS; 
+---------+------+----------------------------------------+ 
| Level | Code | Message        | 
+---------+------+----------------------------------------+ 
| Warning | 1292 | Truncated incorrect DOUBLE value: 'AL' | 
| Warning | 1292 | Truncated incorrect DOUBLE value: 'AK' | 
| Warning | 1292 | Truncated incorrect DOUBLE value: 'CA' | 
| Warning | 1292 | Truncated incorrect DOUBLE value: 'WY' | 
+---------+------+----------------------------------------+ 
4 rows in set (0.00 sec) 

mysql> SELECT 'AL' = 0 /* FALSE */, 'AL' = 1 /* TRUE */; 
+----------+----------+ 
| 'AL' = 0 | 'AL' = 1 | 
+----------+----------+ 
|  1 |  0 | 
+----------+----------+ 
1 row in set, 2 warnings (0.00 sec) 

mysql> SHOW WARNINGS; 
+---------+------+----------------------------------------+ 
| Level | Code | Message        | 
+---------+------+----------------------------------------+ 
| Warning | 1292 | Truncated incorrect DOUBLE value: 'AL' | 
| Warning | 1292 | Truncated incorrect DOUBLE value: 'AL' | 
+---------+------+----------------------------------------+ 
2 rows in set (0.00 sec) 

另一種選擇(更詳細的,但沒有警告):

mysql> SELECT 
    -> `state`, 
    -> CASE `state` 
    ->  WHEN IF(`state` IN ('AL', 'AK'), `state`, NULL) THEN 0.04 
    ->  WHEN 'CA' THEN 0.06 
    ->  WHEN 'WY' THEN 0.03 
    -> END `result` 
    -> FROM `tbl_sta`; 
+-------+--------+ 
| state | result | 
+-------+--------+ 
| AL | 0.04 | 
| AK | 0.04 | 
| CA | 0.06 | 
| WY | 0.03 | 
+-------+--------+ 
4 rows in set (0.00 sec) 

而另一種選擇:

mysql> SELECT 
    -> `state`, 
    -> CASE `state` 
    ->  WHEN CASE WHEN `state` IN ('AL', 'AK') 
    ->   THEN `state` END THEN 0.04 
    ->  WHEN 'CA' THEN 0.06 
    ->  WHEN 'WY' THEN 0.03 
    -> END `result` 
    -> FROM `tbl_sta`; 
+-------+--------+ 
| state | result | 
+-------+--------+ 
| AL | 0.04 | 
| AK | 0.04 | 
| CA | 0.06 | 
| WY | 0.03 | 
+-------+--------+ 
4 rows in set (0.00 sec) 

請參閱db-fiddle

+0

這可以工作。非常感謝您的細心回覆!你真好。謝謝! – Benson

相關問題