0

mysql では、数値 (integer、decimal、または float) 列がある場合、次のsum関数を使用してすべての値を追加できます。

select sum(mycolumn) from mytable where condition;

列の値のに興味があるとします。たとえば、与えられた

+----------+
| mycolumn |
+----------+
|        1 |
|        2 |
|        1 |
|        3 |
+----------+

select fcn(mycolumn) from mytable、私は戻りたいと思い6ます(いくつかの機能のためにfcn)。どうすればいいですか?行数が多い場合がありますのでご注意ください。

4

1 に答える 1

1

正と負の数だけでなく、ゼロでも機能します。

select 
case 
when sum(case when mycolumn = 0 then 1 else 0 end) > 0 then 0 
else
 case when mod(sum(case when mycolumn < 0 then 1 else 0 end),2) = 0 then 1 
 else -1 
 end * exp(sum(log(coalesce(mycolumn,1)))) 
end as product
from mytable
where condition;

これはかなり見栄えが悪いので、毎回クエリを掘り下げる必要がないように、すべてをユーザー定義関数にぶつける価値があるかもしれません。

于 2013-02-14T17:26:25.810 に答える