SQL 2 table 列之间的差异
SQL difference between 2 table columns
场景:
一家工厂每天生产各种尺寸和规格 (swg) 的钢管,记录并存储在 pipe_production table(pipe_id 管道外键 table).工厂也有定期向其销售管道的客户,根据要求创建发票并记录在发票中 table。随后,与每个发票相关的相关管道存储在pipe_invoices table中。当销售团队的一名成员授权发票时,发票 table 中的授权列从 false 切换为 true(0 => 1 英寸),并表示这些管道将被出售并应从库存中移除.
我正在寻找生成库存的查询 table 以在现场准确评估管道。然而,我只是想找出生产的管道和授权发票管道之间的区别。
应用程序正在 Laravel 框架上构建。
Table: 管道
id | description | swg | min_weight | max_weight
1 | 2" X 2" | 16 | 10 | 11
2 | 2" X 2" | 18 | 8 | 19
3 | 1" X 2" | 18 | 4 | 6
Table: pipe_productions
id | pipe_id | quantity | production_date
1 | 1 | 1000 | 2020-10-1
2 | 2 | 2000 | 2020-10-1
3 | 3 | 5500 | 2020-10-1
Table:发票
id | client_id | authorised | loaded | invoice_date
1 | 1 | 0 | 0 | 2020-10-09
2 | 2 | 1 | 0 | 2020-10-09
3 | 2 | 1 | 1 | 2020-10-09
Table: pipe_invoices
id | invoice_id | pipe_id | quantity
1 | 1 | 3 | 2000
2 | 1 | 1 | 1000
3 | 2 | 2 | 1000
编辑:
我的当前查询仅获取 pipe_production 和 pipe_invoices 之间的差异。它不考虑发票未经授权且不应删除的情况。
SELECT *, coalesce(a.quantity, 0)-coalesce(b.quantity, 0) as diff
FROM
(SELECT pipe_id, sum(quantity) as quantity
FROM pipe_productions
GROUP BY pipe_id) a
LEFT JOIN
(SELECT pipe_id, sum(quantity) as quantity
FROM pipe_invoices
GROUP BY pipe_id) b
on a.pipe_id = b.pipe_id
LEFT JOIN pipes
on a.pipe_id = pipes.id
WHERE coalesce(a.quantity, 0)-coalesce(b.quantity, 0) != 0
ORDER BY swg asc, pipe_description desc
我假设您只需要对您的查询进行少量调整,并在您的 b
语句中加入 invoices
:
SELECT *, coalesce(a.quantity, 0)-coalesce(b.quantity, 0) as diff
FROM
(
SELECT pipe_id, sum(quantity) as quantity
FROM pipe_productions
GROUP BY pipe_id
) a
LEFT JOIN
(
SELECT pipe_id, sum(quantity) as quantity
FROM pipe_invoices pi
JOIN invoices i ON pi.invoice_id = i.id
WHERE i.authorised = 1
GROUP BY pipe_id
) b
on a.pipe_id = b.pipe_id
LEFT JOIN pipes
on a.pipe_id = pipes.id
WHERE coalesce(a.quantity, 0)-coalesce(b.quantity, 0) != 0
ORDER BY swg asc, pipe_description desc
场景:
一家工厂每天生产各种尺寸和规格 (swg) 的钢管,记录并存储在 pipe_production table(pipe_id 管道外键 table).工厂也有定期向其销售管道的客户,根据要求创建发票并记录在发票中 table。随后,与每个发票相关的相关管道存储在pipe_invoices table中。当销售团队的一名成员授权发票时,发票 table 中的授权列从 false 切换为 true(0 => 1 英寸),并表示这些管道将被出售并应从库存中移除.
我正在寻找生成库存的查询 table 以在现场准确评估管道。然而,我只是想找出生产的管道和授权发票管道之间的区别。
应用程序正在 Laravel 框架上构建。
Table: 管道
id | description | swg | min_weight | max_weight
1 | 2" X 2" | 16 | 10 | 11
2 | 2" X 2" | 18 | 8 | 19
3 | 1" X 2" | 18 | 4 | 6
Table: pipe_productions
id | pipe_id | quantity | production_date
1 | 1 | 1000 | 2020-10-1
2 | 2 | 2000 | 2020-10-1
3 | 3 | 5500 | 2020-10-1
Table:发票
id | client_id | authorised | loaded | invoice_date
1 | 1 | 0 | 0 | 2020-10-09
2 | 2 | 1 | 0 | 2020-10-09
3 | 2 | 1 | 1 | 2020-10-09
Table: pipe_invoices
id | invoice_id | pipe_id | quantity
1 | 1 | 3 | 2000
2 | 1 | 1 | 1000
3 | 2 | 2 | 1000
编辑: 我的当前查询仅获取 pipe_production 和 pipe_invoices 之间的差异。它不考虑发票未经授权且不应删除的情况。
SELECT *, coalesce(a.quantity, 0)-coalesce(b.quantity, 0) as diff
FROM
(SELECT pipe_id, sum(quantity) as quantity
FROM pipe_productions
GROUP BY pipe_id) a
LEFT JOIN
(SELECT pipe_id, sum(quantity) as quantity
FROM pipe_invoices
GROUP BY pipe_id) b
on a.pipe_id = b.pipe_id
LEFT JOIN pipes
on a.pipe_id = pipes.id
WHERE coalesce(a.quantity, 0)-coalesce(b.quantity, 0) != 0
ORDER BY swg asc, pipe_description desc
我假设您只需要对您的查询进行少量调整,并在您的 b
语句中加入 invoices
:
SELECT *, coalesce(a.quantity, 0)-coalesce(b.quantity, 0) as diff
FROM
(
SELECT pipe_id, sum(quantity) as quantity
FROM pipe_productions
GROUP BY pipe_id
) a
LEFT JOIN
(
SELECT pipe_id, sum(quantity) as quantity
FROM pipe_invoices pi
JOIN invoices i ON pi.invoice_id = i.id
WHERE i.authorised = 1
GROUP BY pipe_id
) b
on a.pipe_id = b.pipe_id
LEFT JOIN pipes
on a.pipe_id = pipes.id
WHERE coalesce(a.quantity, 0)-coalesce(b.quantity, 0) != 0
ORDER BY swg asc, pipe_description desc