在mysql语句中组合Concat

时间:2016-08-08 12:46:06

标签: mysql select

我有一张名为delitems的表,里面有一些列。

在我的SELECT声明中,我想使用GROUP_CONCAT

 +-------------------------------+-------+--------+--------+-----+
 | COLOR                         | tOTAL | Ptotal | Amount | qty |
 +-------------------------------+-------+--------+--------+-----+
 | BLUE - W = 55,BLUE - W/O = 93 |   148 |    375 |  55500 |   2 |
 +-------------------------------+-------+--------+--------+-----+
 mysql>select GROUP_CONCAT(color,' = ',qty) as COLOR, SUM(qTY) AS tOTAL, suM(p_cost)  as Ptotal, SUM(qty)*SUM(p_cost) as Amount,count(*) qty from delitems where status='3' Group By cont_no;

Amount列之外,一切正常。总金额错了!这是正确的值:

+-----------------+-------+--------+--------+-----+
| COLOR           | tOTAL | Ptotal | Amount | qty |
+-----------------+-------+--------+--------+-----+
| BLUE - W = 55   |    55 |    125 |   6875 |   1 |
| BLUE - W/O = 93 |    93 |    250 |  23250 |   1 |
+-----------------+-------+--------+--------+-----+
mysql>select GROUP_CONCAT(color,' = ',qty) as COLOR, SUM(qTY) AS tOTAL, suM(p_cost) as Ptotal, SUM(qty)*SUM(p_cost) as Amount,count(*) qty from delitems where status='3' Group By color;

我只想在一行中显示正确的总金额

请帮忙。

1 个答案:

答案 0 :(得分:1)

你应该需要总和(a * b)而不是总和(a)*总和(b)

select 
   GROUP_CONCAT(color,' = ',qty) as COLOR
 , SUM(qTY) AS tOTAL
 , suM(p_cost)  as Ptotal
 , SUM(qty*(p_cost) as Amount, count(*) qty 
from delitems 
where status='3' Group By cont_no;