我正在尝试为指定客户选择折扣列表,但如果客户为该商品指定了部件号,则只包含产品组中的最高折扣。
Customers table:
id name
1226 ESPRESSO SPECIALTY FOODS
1227 ESSROC ACCOUNTING SERVICES
1228 EVAPAR
折扣表:
prod_code_id part_num account_id multiplier
123 28H57 1228 0.75
123 1228 0.8
303 1228 0.65
项目表:
prod_code_id item description price
123 28h57 Oil 50.00
123 0072 HOSE 62.31
123 0147 BOLT 0.98
303 0147 BOLT 1.39
303 0211 HEX SOCKET 1.86
这就是我要做的事情
SELECT
items.item,
items.description,
items.price,
discounts.multiplier
FROM items
LEFT JOIN
(
SELECT multiplier,prod_code_id
FROM ".$type."_discounts
WHERE account_id='".$_POST['accountId']."'
ORDER BY part_num collate utf8_general_ci LIKE '%".$_POST['searchPart']."%' DESC
) AS discounts ON items.prod_code_id=discounts.prod_code_id
WHERE items.item collate utf8_general_ci LIKE '%".$search_word."%'
LIMIT 8
因此,假设我输入的子查询中有一个特殊折扣的零件编号:
SELECT prod_code_id, part_num, account_id,multiplier
FROM customers_discounts
WHERE account_id=1228
ORDER BY part_num collate utf8_general_ci LIKE '%28h57%' DESC
关于我得到的输出
prod_code_id part_num account_id multiplier
123 28H57 1228 0.75
123 1228 0.8
303 1228 0.65
当我只想要
时prod_code_id part_num account_id multiplier
123 28H57 1228 0.75
303 1228 0.65
另一方面,如果未指定零件编号或没有特殊折扣,我希望:
prod_code_id part_num account_id multiplier
123 1228 0.8
303 1228 0.65
MYSQL FIDDLE LINK: MYSQL FIDDLE