2015-02-23 132 views
0

我有以下形式的MySQL表:MySQL查詢與條件

id | error 1 | error 2 | error 3 | error 4 | 
    -------------------------------------------- 
    1 | 1 | 0 | 0 | 1 | 
    ------------------------------------------- 
    2 | 0 | 0 | 0 | 0 | 

id是主鍵,並且錯誤是布爾列。 有沒有一種方法可以獲取行的ID,如果所有錯誤都爲零,如果任何一個錯誤爲1,則爲真,如果錯誤爲零。

+0

也許? – 2015-02-23 09:59:03

+0

錯誤列可以有NULL嗎? – jarlh 2015-02-23 10:01:09

+0

參見規範化 - 或存儲字符串,例如:1001 - 或存儲十進制表示法,例如。 9 – Strawberry 2015-02-23 10:11:35

回答

5
select id,case when error1+error2+error3+error4 > 0 
       then 1 
       else 0 
      end as error 
from Table t; 
+0

格式化查詢。 – 2015-02-23 10:00:45

1
select id, case when error1 + error2 + error3 + error4 > 0 
       then 1 
       else 0 
      end as result 
from your_table 
0
SELECT id 
FROM "your table name" 
WHERE error 1 = 1 
OR error 2 = 1 
OR error 3 = 1 
OR error 4 = 1 
OR error 5 = 1 
用WHERE子句