2013-04-26 72 views

回答

8

鑄布爾類型的整數(0=false1=true),並檢查他們的總和:

select * 
from my_table 
where a::int + b::int + c::int + d::int + e::int >= 2; 
+0

這是一些有效的編碼 – 2013-04-27 00:02:18

2

很長的路要走:

SELECT * from t where c1 and c2 or c1 and c3 or c1 and c4 or c1 and c5 
or c2 and c3 or c2 and c4 or c2 and c5 or c3 and c4 or c3 and c5 or c4 and c5; 
1

接受的答案只能假設所有列被定義NOT NULL,沒有被指定。爲了使其與NULL工作,太:

SELECT * 
FROM tbl 
WHERE (a IS TRUE)::int 
    + (b IS TRUE)::int 
    + (c IS TRUE)::int 
    + (d IS TRUE)::int 
    + (e IS TRUE)::int > 1; 

或者:

SELECT * 
FROM tbl 
WHERE COALESCE(a::int, 0) 
    + COALESCE(b::int, 0) 
    + COALESCE(c::int, 0) 
    + COALESCE(d::int, 0) 
    + COALESCE(e::int, 0) > 1;