MYSQL: 只根据group by ID在第一行添加一些数据

MYSQL: Add some data in the first row only based on group by ID

我有这组样本数据:

Table invoice:

--------------------------------------------------
| ID |     date     | invoice_number |   total   |
--------------------------------------------------
| 81 |  2017-03-24  |   0000000173   |  190.00   |
--------------------------------------------------

Table invoice_addon:

----------------------------------------------------------
| ID |  invoice_id  |       description       |  amount  |
----------------------------------------------------------
| 46 |      81      | Price Adjust. - Jumbo   |  -12.00  |
| 47 |      81      | Price Adjust. - Regular |  -12.00  |
----------------------------------------------------------

Table orders:

----------------------------------------------------------------------------------
| ID  |  invoice_id  |  box_name   |   size   |  price  |   tax   |  box_number  |
----------------------------------------------------------------------------------
| 177 |      81      | Jumbo Box   | 23x25x17 |  97.00  |  15.00  |  FCI107056   |
| 178 |      81      | Regular Box | 20x23x17 |  87.00  |  15.00  |  FCI107057   |
----------------------------------------------------------------------------------

我想达到的目标:

----------------------------------------------------------------------------------------------------------------------------------
| trans_date  | inv_number |  box_name   |   size   |  gross   |  box_number  | others | description                     |  net  |
----------------------------------------------------------------------------------------------------------------------------------
| 2017-03-24  | 0000000173 | Jumbo Box   | 23x25x17 |  112.00  |  FCI107056   | -24.00 |  Price Adjust. - Jumbo -12.00   | 88.00 |
|             |            |             |          |          |              |        |  Price Adjust. - Regular -12.00 |       |
----------------------------------------------------------------------------------------------------------------------------------
| 2017-03-24  | 0000000173 | Regular Box | 20x23x17 |  102.00  |  FCI107057   |      0 |  NULL                           | 102.00|
----------------------------------------------------------------------------------------------------------------------------------

我当前的查询:

SELECT DATE(i.date) AS trans_date, i.invoice_number AS inv_number, o.box_name as box_name, 
o.size AS size, (SELECT Price + Tax FROM orders WHERE ID = o.ID) AS gross, 
o.box_number AS box_number,
SUM(a.amount) AS others, 
(SELECT GROUP_CONCAT(CONCAT(description, ' ', amount) SEPARATOR '<br />') FROM invoice_addon WHERE invoice_id = i.ID) AS description, 
(SUM(o.price + o.tax) + SUM(a.amount)) AS net 
FROM `invoice` i 
INNER JOIN orders o ON i.ID = o.invoice_id 
LEFT JOIN invoice_addon a ON i.ID = a.invoice_id 
WHERE i.ID = 81
GROUP BY o.ID

我的查询结果中的问题是,othersdescription 列加倍了。它应该只显示在第一行。无论发票中捆绑了多少个箱子,它们都应该只添加在第一行。网络也依赖于这些列。

我得到的:

----------------------------------------------------------------------------------------------------------------------------------
| trans_date  | inv_number |  box_name   |   size   |  gross   |  box_number  | others | description                     |  net   |
----------------------------------------------------------------------------------------------------------------------------------
| 2017-03-24  | 0000000173 | Jumbo Box   | 23x25x17 |  112.00  |  FCI107056   | -24.00 |  Price Adjust. - Jumbo -12.00   | 200.00 |
|             |            |             |          |          |              |        |  Price Adjust. - Regular -12.00 |        |
----------------------------------------------------------------------------------------------------------------------------------
| 2017-03-24  | 0000000173 | Regular Box | 20x23x17 |  102.00  |  FCI107057   | -24.00 |  Price Adjust. - Jumbo -12.00   | 180.00 |
|             |            |             |          |          |              |        |  Price Adjust. - Regular -12.00 |        |
----------------------------------------------------------------------------------------------------------------------------------

可能吗?我怎样才能做到这一点? (我在 MySQL 中使用 CodeIgniter 和 DataTable 执行此操作)

那么,您需要一些 var 来存储当前行号并检查它是否等于 1。像这样:

SET @row_number = 0;
SELECT DATE(i.date) AS trans_date, i.invoice_number AS inv_number, o.box_name as box_name, 
o.size AS size, (SELECT Price + Tax FROM orders WHERE ID = o.ID) AS gross, 
o.box_number AS box_number,
if((@row_number:=@row_number + 1)=1,SUM(a.amount), null) AS others, 
if(@row_number=1,(SELECT GROUP_CONCAT(CONCAT(description, ' ', amount) 
SEPARATOR '<br />') FROM invoice_addon WHERE invoice_id = i.ID), null) AS description, 
(SUM(o.price + o.tax) + SUM(a.amount)) AS net 
FROM `invoice` i 
INNER JOIN orders o ON i.ID = o.invoice_id 
LEFT JOIN invoice_addon a ON i.ID = a.invoice_id  
WHERE i.ID = 81
GROUP BY o.ID