mysql从现有查询中的另一个表中获取列值

时间:2016-02-17 08:30:44

标签: mysql subquery

我有三个MySQL表,成功完成了一个查询,该查询会根据订单显示付款表上的一些计算。

问题:  在下面提到的查询连接条件是

时,需要从sku_mapping表中获取full_name列

concat(' f |',fk_orders.sku)= sku_mapping.prefix_sku

fk_orders

migrate

fk_payments

|order_item_id |order_id    |Invoice_No       |Invoice_No_Amt  |Qty   |Refund_Qty |Refund_Amount | sku
------------------------------------------------------------------------------------------------------
|1131231       |123         |F08OTTN16-1      |100            |1     |            |              |A3001
|1113138       |321         |F08OTTN16-2      |200            |2     |1           |200           |B1001
|1231231       |023         |F08OTTN16-3      |100            |1     |1           |100           |C2001
|1133138       |320         |F08OTTN16-4      |200            |2     |            |              |D8901
|1134231       |103         |F08OTTN16-5      |100            |1     |            |              |E6210
|1113538       |300         |F08OTTN16-6      |200            |2     |            |              |F1001
|1003538       |300         |F08OTTN16-7      |200            |2     |            |              |G9003

sku_mapping

|order_item_id    |order_id    |Invoice_No       |Invoice_No_Amt |Settlement_Value
-----------------------------------------------------------------------------------
|OI:1131231       |123         |F08OTTN16-1      |100            |40
|OI:1113138       |321         |F08OTTN16-2      |200            |150
|OI:1231231       |023         |F08OTTN16-3      |100            |-50
|OI:1133138       |320         |F08OTTN16-4      |200            |200
|OI:1134231       |103         |F08OTTN16-5      |100            |40
|OI:1113538       |300         |F08OTTN16-6      |200            |250
|OI:1131231       |123         |F08OTTN16-1      |100            |40
|OI:1133138       |320         |F08OTTN16-4      |200            |100
|OI:1113138       |321         |F08OTTN16-2      |200            |-200

当前结果

|prefix_sku |full_name 
-------------------
x|A3001     |Apple_Phone
f|B1001     |Belkin
f|C2001     |Cat_Access
f|D8901     |Dlink
f|E6210     |Eltron
f|F1001     |Flag
f|G9003     |gott
f|A3001     |Phone
a|B1001     |Belkin
a|C2001     |Cat_Access
a|D8901     |Dlink
a|E6210     |Eltron
a|F1001     |Flag
a|G9003     |gott

预期结果(需要来自sku_prefix表的full_name列)

|order_item_id  |order_id   |Invoice_No     |Invoice_No_Amt |Qty    |Refund_Qty |Refund_Amount  |sku    |SettledAmount  |netAmount
------------------------------------------------------------------------------------------------------------------------------------
|1131231        |123        |F08OTTN16-1    |100            |1      |           |               |A3001  |80             |20
|1113138        |321        |F08OTTN16-2    |200            |2      |1          |200            |B1001  |150            |50
|1231231        |23         |F08OTTN16-3    |100            |1      |1          |100            |C2001  |50             |50 
|1133138        |320        |F08OTTN16-4    |200            |2      |           |               |D8901  |300            |-100
|1134231        |103        |F08OTTN16-5    |100            |1      |           |               |E6210  |40             |60
|1113538        |300        |F08OTTN16-6    |200            |2      |           |               |F1001  |250            |-50
|1003538        |300        |F08OTTN16-7    |200            |2      |           |               |G9003  |0              |200

当前代码

|order_item_id  |order_id   |Invoice_No     |Invoice_No_Amt |Qty    |Refund_Qty |Refund_Amount  |sku    |SettledAmount  |netAmount  |full_name
-----------------------------------------------------------------------------------------------------------------------------------------------
|1131231        |123        |F08OTTN16-1    |100            |1      |           |               |A3001  |80             |20         |Apple_Phone
|1113138        |321        |F08OTTN16-2    |200            |2      |1          |200            |B1001  |150            |50         |Belkin
|1231231        |23         |F08OTTN16-3    |100            |1      |1          |100            |C2001  |50             |50         |Cat_Access
|1133138        |320        |F08OTTN16-4    |200            |2      |           |               |D8901  |300            |-100       |Dlink
|1134231        |103        |F08OTTN16-5    |100            |1      |           |               |E6210  |40             |60         |Eltron
|1113538        |300        |F08OTTN16-6    |200            |2      |           |               |F1001  |250            |-50        |Flag
|1003538        |300        |F08OTTN16-7    |200            |2      |           |               |G9003  |0              |200        |gott

1 个答案:

答案 0 :(得分:1)

假设fk_orders有一个sku字段。

SELECT o.*, sk.full_name, (coalesce(Refund_Amount, 0) + coalesce(sv, 0)) AS SettledAmount, (Invoice_No_Amt - coalesce(Refund_Amount, 0) - coalesce(sv, 0)) AS netAmount
FROM fk_orders o 
LEFT JOIN (SELECT invoice_no, SUM(Settlement_Value) AS sv
           FROM fk_payments
           GROUP BY invoice_no) p ON o.invoice_no = p.invoice_no
INNER JOIN sku_mapping sk ON o.sku = SUBSTRING_INDEX(prefix_sku,'|',-1)