我有三个表格(发票,费率,费率拍打) 我在发票表中有重量和no_of_package 如果invoice.weight <= rate.weight,则它将返回该表rate.weight 否则weight <= rate_slap.weight然后它将返回该表rate_slap.weight
但不从两个表中返回任何一个表值。
SELECT
rate.`minimum_wgt`,
slap.`slap_weight`
FROM m_ac_customer_rate rate
LEFT JOIN m_ac_customer_rate_slap slap ON (rate.minimum_wgt<=15)
OR (slap.slap_weight<=15)
WHERE rate.customer_rate_id=slap.customer_rate_master
AND `customer_name`=1007
AND `destination_name`=3
AND `service_type`=1
AND `shipment_type`='D'
AND `payment_mode`='CASH';
样本数据
发票表
invoice_no,weight,total_carton
2142423, 10.4, 5
费率表
invoice_no,weight,rate_per_kg
2142423 , 15.8, 150.00
rate_slap表
rateslap_id,weight,rate_per_kg
2142423 , 10.8, 10.00
答案 0 :(得分:0)
这将在子查询中选择两个权重(如果存在)以及发票权重,然后在主查询中执行CASE ...以选择正确的权重
SELECT no_of_package,
CASE WHEN weight <= rate_weight THEN rate_weight
WHEN weight <= slap_weight THEN slap_weight
ELSE 0
END AS other_weight
FROM
(SELECT i.weight, IFNULL(r.weight, 0) as rate_weight, IFNULL(rs.weight, 0) as slap_weight
FROM invoice I
LEFT JOIN rate r ON r.invoice_no = i.invoice_no AND minimum_wgt <= 15
LEFT JOIN rate_slap rs ON rs.invoice_no = i.invoice_no AND slap_weight <= 15
WHERE customer_name = 1007
AND destination_name = 3
AND service_type = 1
AND shipment_type = 'D'
AND payment_mode = 'CASH')
我为CASE选择了默认值0,也许应该是发票的重量。您需要调整表名称,我使用了名称的简短版本