我有1个表有很多记录,我想分开COUNT(*)
值(盈利/亏损),我可以用2个MySQL查询来做,但我想用1个查询来做那可能吗?
$query_string = '
SELECT
MONTH(order_time) AS month,
CASE WHEN order_profit > 0 THEN COUNT(*) END AS profit,
CASE WHEN order_profit < 0 THEN COUNT(*) END AS loss,
COUNT(*) AS total
FROM
trade_histories
';
$query_string .= '
WHERE
order_profit IS NOT NULL
';
$query_string .= '
GROUP BY
MONTH(order_time)
';
这是我到目前为止所尝试的,这就是结果
stdClass Object ( [month] => 1 [profit] => 447 [loss] => [total] => 447 )
stdClass Object ( [month] => 2 [profit] => 1153 [loss] => [total] => 1153 )
stdClass Object ( [month] => 3 [profit] => 898 [loss] => [total] => 898 )
stdClass Object ( [month] => 4 [profit] => [loss] => 640 [total] => 640 )
stdClass Object ( [month] => 12 [profit] => [loss] => 2 [total] => 2 )