微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?