【发布时间】:2020-10-07 21:12:51
【问题描述】:
我有这组样本数据:
表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
我的查询结果中的问题是,others 和 description 列加倍。它应该只显示在第一行。不管在发票上捆绑了多少个箱子,都应该只在第一行添加。网络也依赖于这些列。
我得到了什么:
----------------------------------------------------------------------------------------------------------------------------------
| 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 执行此操作)
【问题讨论】:
-
主要问题是invoice_addon 没有链接,因此例程知道它与哪个框相关。你应该重新设计你的方法
-
@nbk 只要它们仅在第一个数据中列出就没有关系。总是。
invoice_id是否对orders表和invoice_addon都通用,无法实现这一点? -
由于您没有为 invoice_addon.id 定义关系,所以没有算法知道应该在哪里添加发票税,所以建立 ar 关系,一切都很容易
标签: mysql codeigniter datatables correlated-subquery