MYSQL:仅根据group by ID在第一行添加一些数据
我有这组样本数据: 表格发票: 表1\u插件: 表顺序: 我想要达到的目标:MYSQL:仅根据group by ID在第一行添加一些数据,mysql,codeigniter,datatables,correlated-subquery,Mysql,Codeigniter,Datatables,Correlated Subquery,我有这组样本数据: 表格发票: 表1\u插件: 表顺序: 我想要达到的目标: ---------------------------------------------------------------------------------------------------------------------------------- | trans_date | inv_number | box_name | size | gross | box_number |
----------------------------------------------------------------------------------------------------------------------------------
| 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
可能吗?我怎样才能做到这一点?
我在MySQL中使用CodeIgniter和DataTable来实现这一点,您需要一些可以存储当前行号并检查其是否等于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
主要的问题是没有invoice_插件的链接,因此例程知道它与哪个框相关。你应该重新找回你的梦想approach@nbk只要他们只在第一个数据上列出,这并不重要。总是发票id对于orders表和invoice\u addon都是通用的吗?无法做到这一点?因为您没有为invoice\u addon.id定义关系,所以没有算法知道应该在何处添加发票税,所以建立ar关系,一切都很容易。谢谢您的回答。它适用于特定的数据i.ID=81,但我希望使用它时不带where子句,因此在每次出现新发票时,都应填写其他和说明。\u ID.@Elimariaa从您的question@elimariaaa将最后一张发票的id存储在某个var中,并检查是否等于新发票的id是,对不起。谢谢
----------------------------------------------------------------------------------------------------------------------------------
| 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
----------------------------------------------------------------------------------------------------------------------------------
| 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 | |
----------------------------------------------------------------------------------------------------------------------------------
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