MYSQL:仅根据ID分组在第一行中添加一些数据

问题描述

我有这组示例数据:

invoice

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

invoice_addon

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

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 |        |
----------------------------------------------------------------------------------------------------------------------------------

有可能吗?我该怎么做? (我正在使用CodeIgniter和DataTable在MysqL中进行此操作)

解决方法

好吧,您需要一些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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...