0

我正在尝试为指定客户选择折扣列表,但如果客户为该项目指定了部件号,则仅包括产品组中的一个最高折扣。

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 链接: MYSQL FIDDLE

4

0 回答 0