问题
I have three mysql tables have achieved to have all invoices from the orders table along with Invoice_No_Amt - Refund_Amount, group sum of settlement value
Now I would like to have fullname column from sku_mapping table also to be listed in the result. prefix_sku is the primary key in sku_mapping table
fk_orders
|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
fk_payments
|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
sku_mapping
|prefix_sku |full_name
-------------------
f|A3001 |Apple_Phone
f|B1001 |Belkin
f|C2001 |Cat_Access
f|D8901 |Dlink
f|E6210 |Eltron
f|F1001 |Flag
f|G9003 |gott
a|A3001 |Apple_Phone
a|B1001 |Belkin
a|C2001 |Cat_Access
a|D8901 |Dlink
a|E6210 |Eltron
a|F1001 |Flag
a|G9003 |gott
query
select o.*,
(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;
guess this is statement be incorporated in the above code to get prefix_sku from sku_mapping table but dono how to have where statement in join
concat ('f|',O.f|sku
)=conso.conso
回答1:
So if you want to add "where condition" in join , you can do it by specifying as a join criteria, for example
select *
from A
join B
on A.id = B.id
and B.id > 4000 ;
来源:https://stackoverflow.com/questions/35435228/join-multiple-mysql-table-with-prefix-and-group-sum