2013-04-25 43 views
2

另一個MYSQL的問題,我找不到答案:當滿足至少x個colums中的條件時,MYSQL會選擇行嗎?

我有這張表,我想要得到所有ID的SN> 7和代表> 4,並在列1-6中的某些條件滿足至少x次。

例如,其中至少3個細胞在列COL1-COL6具有值> 1.

第一部分是容易的(SELECT * FROM table WHERE SN > 7 AND reps > 4....),但我不能找出第二部分。

謝謝!

ID SN reps col1 col2 col3 col4 col5 col6 
A 12 3  0.6 1  3  -2 1  3 
B 6  5  3.2 1.1 -3.3 3  0  0 
C 300 6  1.3 -0.4 0  0.6 -0.5 -3.3 
+0

我猜你需要的第二部分,以澄清 – jcho360 2013-04-25 19:44:53

回答

3

嘗試:

SELECT * FROM table 
WHERE SN > 7 AND reps > 4 and 
     (case when `1` > 1 then 1 else 0 end + 
     case when `2` > 1 then 1 else 0 end + 
     case when `3` > 1 then 1 else 0 end + 
     case when `4` > 1 then 1 else 0 end + 
     case when `5` > 1 then 1 else 0 end + 
     case when `6` > 1 then 1 else 0 end) >= 3 
+0

這幫助。謝謝 – user2317711 2013-04-25 20:56:03

1
select * from table where sn> 7 and reps > 4 and 
(((`1` > 1) + (`2` > 1) + (`3` > 1) + (`4` > 1) + (`5` >1) + (`6` > 1)) >= 3) 
+0

也不錯。謝謝 – user2317711 2013-04-25 20:56:28

相關問題