✔ 最佳答案
count只能對單一個field數。即數e1有幾個個2。
指令係 select count(*) from table where e1=2,
再加e2,即係select count(*) from table where e2=2,
再加e3,即係select count(*) from table where e3=2,
但似乎沒有指令可以相加。
方法一係以程式碼把三個得出數字相加。
Mm...
要把相加在sql完成,也許可以先把三個field組成一個單一table,即
select name,e1 as num from table
union
select name,e2 as num from table
union
select name,e3 as num from table
,然後用先前select count(*) from (select ....) where num=2
不過sql未必接受呢個句式(你可以試一下)。
要變化的話,可以將union那句化成一個view。那麼select count(*) from MyView where num=2一定得。
如果你用的sql沒有view,你可以把第一句結果存到一個temp table。
如果也不支援temp table就‧‧‧‧‧
另一個注意係效率低,即係如果record多會好慢。