2017-04-03 95 views
0

我有一個條件,即MySQL中的某個字段可以具有相同的值(端口號),但可以根據主機ID代表兩個不同的設備。我有以下,但它似乎並沒有工作:MySQL case語句中的額外列條件不起作用

SELECT s.name AS 'ingester', 
    d.dev_used AS 'size', 
    Cast(CASE d.namespace_port 
     (WHEN 4015 AND d.das_station IN(11,22,23)) THEN 'BPN1' 
     (WHEN 4015 AND d.das_station NOT IN(11,22,23)) THEN 'SJC12' 
     WHEN 4016 THEN 'SJC17' 
     WHEN 4017 THEN 'SJC18' 
     WHEN 4018 THEN 'SJC19' 
     WHEN 4019 THEN 'SJC5' 
     WHEN 4020 THEN 'SJC6' 
     WHEN 4021 THEN 'SJC7' 
     WHEN 4022 THEN 'SJC8' 
     WHEN 4023 THEN 'SJC9' 
     WHEN 4024 THEN 'SJC10' 
     WHEN 4025 THEN 'SJC11' 
     WHEN 4026 THEN 'SJC20' 
     WHEN 4027 THEN 'SJC13' 
     WHEN 4028 THEN 'SJC14' 
     WHEN 4029 THEN 'SJC15' 
     WHEN 4030 THEN 'SJC16' 
    END AS CHAR) AS namespace, 
    Date_format(d.date_attached, '%b-%e-%Y %l:%i %p') AS 'date_attached', 
    Date_format(d.replication_started, '%b-%e-%Y %l:%i %p') AS 'date_started', 
    Date_format(d.replication_completed, '%b-%e-%Y %l:%i %p') AS 'date_finished', 
    Date_format(d.date_detached, '%b-%e-%Y %l:%i %p') AS 'date_detached' 
FROM das.stations s, 
     das.device d 
WHERE d.das_station = s.id 
AND svcid = "trrq" 
ORDER BY d.date_attached DESC 

閱讀MYSQL CASE STATEMENT MULTIPLE CONDITIONS,看來這應該是可能的,但是當我嘗試和執行查詢,我得到如下:

ERROR 1064 (42000): 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 '4015 AND d.das_station IN(11,22,23)) THEN 'BPN1' 
4015 AND d.das_station NOT IN (1' at line 4 

在像這樣的case語句中不可能有AND條件嗎?

編輯:看來粘貼到MySQL不喜歡查詢

mysql> SELECT s.name AS 'ingester', 
    ->  d.dev_used AS 'size', 
    ->  Cast(CASE d.namespace_port 
    -> 
Display all 891 possibilities? (y or n) 
    -> (4015 AND d.das_station IN (11,22,23)) THEN 'BPN1' 
    -> 
Display all 891 possibilities? (y or n) 
    -> (4015 AND d.das_station NOT IN (11,22,23)) THEN 'SJC12' 
    ->   WHEN 4016 THEN 'SJC17' 
    ->   WHEN 4017 THEN 'SJC18' 
    ->   WHEN 4018 THEN 'SJC19' 
    ->   WHEN 4019 THEN 'SJC5' 
    ->   WHEN 4020 THEN 'SJC6' 
    ->   WHEN 4021 THEN 'SJC7' 
    ->   WHEN 4022 THEN 'SJC8' 
    ->   WHEN 4023 THEN 'SJC9' 
    ->   WHEN 4024 THEN 'SJC10' 
    ->   WHEN 4025 THEN 'SJC11' 
    ->   WHEN 4026 THEN 'SJC20' 
    ->   WHEN 4027 THEN 'SJC13' 
    ->   WHEN 4028 THEN 'SJC14' 
    ->   WHEN 4029 THEN 'SJC15' 
    ->   WHEN 4030 THEN 'SJC16' 
    ->  END AS CHAR) AS namespace, 
    ->  Date_format(d.date_attached, '%b-%e-%Y %l:%i %p') AS 'date_attached', 
    ->  Date_format(d.replication_started, '%b-%e-%Y %l:%i %p') AS 'date_started', 
    ->  Date_format(d.replication_completed, '%b-%e-%Y %l:%i %p') AS 'date_finished', 
    ->  Date_format(d.date_detached, '%b-%e-%Y %l:%i %p') AS 'date_detached' 
    -> FROM das.stations s, 
    ->  das.device d 
    -> WHERE d.das_station = s.id 
    -> AND svcid = "trrq" 
    -> ORDER BY d.date_attached DESC 

如果我刪除行包,它似乎正確粘貼,但仍然是不尊重的第二場:

mysql> SELECT s.name AS 'ingester', d.das_station, d.dev_used AS 'size',  Cast(CASE d.namespace_port WHEN (4015 AND d.das_station = 11) THEN 'BPN1' WHEN (4015 AND d.das_station NOT IN(11,22,23)) THEN 'SJC12' WHEN 4016 THEN 'SJC17' WHEN 4017 THEN 'SJC18' WHEN 4018 THEN 'SJC19' WHEN 4019 THEN 'SJC5' WHEN 4020 THEN 'SJC6' WHEN 4021 THEN 'SJC7' WHEN 4022 THEN 'SJC8' WHEN 4023 THEN 'SJC9' WHEN 4024 THEN 'SJC10' WHEN 4025 THEN 'SJC11' WHEN 4026 THEN 'SJC20' WHEN 4027 THEN 'SJC13' WHEN 4028 THEN 'SJC14' WHEN 4029 THEN 'SJC15' WHEN 4030 THEN 'SJC16' END AS CHAR) AS namespace,  Date_format(d.date_attached, '%b-%e-%Y %l:%i %p') AS 'date_attached',  Date_format(d.replication_started, '%b-%e-%Y %l:%i %p') AS 'date_started',  Date_format(d.replication_completed, '%b-%e-%Y %l:%i %p') AS 'date_finished',  Date_format(d.date_detached, '%b-%e-%Y %l:%i %p') AS 'date_detached' FROM das.stations s,  das.device d WHERE d.das_station = s.id AND svcid = "trrq" ORDER BY d.date_attached DESC; 
+---------------+-------------+------+-----------+---------------------+--------------+---------------------+---------------------+ 
| ingester  | das_station | size | namespace | date_attached  | date_started | date_finished  | date_detached  | 
+---------------+-------------+------+-----------+---------------------+--------------+---------------------+---------------------+ 
| ca-adsdas2-p1 |   11 | NULL | NULL  | Jan-20-2016 1:42 PM | NULL   | NULL    | Jan-21-2016 3:16 PM | 
| ca-adsdas2-p1 |   11 | NULL | NULL  | Dec-21-2015 7:25 PM | NULL   | Dec-26-2015 9:22 PM | Jan-20-2016 1:33 PM | 
+---------------+-------------+------+-----------+---------------------+--------------+---------------------+---------------------+ 

如果das_station = 11,它應該在命名空間中聲明「BPN1」,而不是null。

回答

0

通過在WHENs的namespace_port,這樣解決:

SELECT s.name AS 'ingester', 
    d.dev_used AS 'size', 
    Cast(CASE WHEN (d.namespace_port = 4015 AND d.das_station IN (11,22,23)) THEN 'BPN1' WHEN (d.namespace_port = 4015 AND d.das_station NOT IN (11,22,23)) THEN 'SJC12' WHEN d.namespace_port = 4016 THEN 'SJC17' WHEN d.namespace_port = 4017 THEN 'SJC18' WHEN d.namespace_port = 4018 THEN 'SJC19' WHEN d.namespace_port = 4019 THEN 'SJC5' WHEN d.namespace_port = 4020 THEN 'SJC6' WHEN d.namespace_port = 4021 THEN 'SJC7' WHEN d.namespace_port = 4022 THEN 'SJC8' WHEN d.namespace_port = 4023 THEN 'SJC9' WHEN d.namespace_port = 4024 THEN 'SJC10' WHEN d.namespace_port = 4025 THEN 'SJC11' WHEN d.namespace_port = 4026 THEN 'SJC20' WHEN d.namespace_port = 4027 THEN 'SJC13' WHEN d.namespace_port = 4028 THEN 'SJC14' WHEN d.namespace_port = 4029 THEN 'SJC15' WHEN d.namespace_port = 4030 THEN 'SJC16' END AS CHAR) AS namespace, 
    Date_format(d.date_attached, '%b-%e-%Y %l:%i %p') AS 'date_attached', 
    Date_format(d.replication_started, '%b-%e-%Y %l:%i %p') AS 'date_started', 
    Date_format(d.replication_completed, '%b-%e-%Y %l:%i %p') AS 'date_finished', 
    Date_format(d.date_detached, '%b-%e-%Y %l:%i %p') AS 'date_detached' 
FROM das.stations s, 
     das.device d 
WHERE d.das_station = s.id 
AND svcid = "96wa" 
ORDER BY d.date_attached DESC 

結果如下:

+------------------+--------------+-----------+---------------------+---------------------+----------------------+---------------------+ 
| ingester   | size   | namespace | date_attached  | date_started  | date_finished  | date_detached  | 
+------------------+--------------+-----------+---------------------+---------------------+----------------------+---------------------+ 
| bpn-dasingest-p2 |   NULL | NULL  | Mar-29-2017 7:41 AM | NULL    | NULL     | Mar-30-2017 8:58 AM | 
| bpn-dasingest-p2 | 247547875328 | BPN1  | Mar-27-2017 7:55 AM | Mar-27-2017 9:33 AM | Mar-27-2017 12:27 PM | Mar-29-2017 7:41 AM | 
+------------------+--------------+-----------+---------------------+---------------------+----------------------+---------------------+ 
+0

您俯瞰所做的顯著變化,爲了解決這個問題。您已完全切換[CASE' expressiobs的兩種樣式](https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html)。你從'CASE值WHEN [compare_value] THEN result ...'切換到'CASE WHEN [condition] THEN result ...'。在你的原始文件中,「WHEN」後面跟着一個*值進行比較*。 '(4015 AND d.das_station NOT IN(11,22,23))'是一個邏輯表達式,它只能等於1或0,它不會以任何有意義的方式匹配namespace_port的值。 –

+0

@ Michael-sqlbot感謝您的評論。這實際上是我如何發現它的......在審查了我發佈的第一個鏈接後,我注意到我正試圖在整個聲明中包含第二個字段,而不是如何在以前的帖子中包含每個WHEN聲明中的兩個條件,而不是CASE聲明。我的第二個問題是,由於某些原因,雖然PHP可以用空格讀取整個文件,但複製+粘貼並不像MySQL客戶端那樣寬容,而是剝離了我的「WHEN」 –