【问题标题】:get product price based on customer code根据客户代码获取产品价格
【发布时间】:2013-09-07 19:49:34
【问题描述】:

我的产品表看起来像

ID     Type          Product Code     Product Name     Price
1      all customers 56620000         Product A        219.68 €
2      all customers 56621000         Product B        4,351.91 €
3      all customers 56622000         Product C        110.98 €
4      155000        56622000         Product C        100.00 €
5      all customers 56626000         Product D        2,410.38 €
6      all customers 56772000         Product E        100.00 €
7      160000        56772000         Product E        90.00 €

如果您注意到第 3,4 行和第 6,7 行具有相同的产品代码,但类型和价格不同。这意味着产品可以为所有客户以及少数特定客户定价。如果客户 ID 为 155000 的客户使用产品代码 56622000 执行搜索,则该客户将获得 100.00 欧元(参见第 4 行)而不是 110.98 欧元(参见第 3 行)的价格。但是,如果同一位客户使用产品代码 56772000 进行搜索,他/她将获得 100.00 欧元(见第 6 行)而不是 90.00 欧元(见第 7 行)的价格。因为该用户没有针对产品代码 56772000 的具体价格。

我的查询:如何使用 PHP 和 MySql 从单个表执行此操作。?

【问题讨论】:

  • 您想向客户展示什么?两个值还是一个?
  • Type 列的类型是什么?
  • Type 列包含All CustomerAll Customers
  • @blo:我想在搜索结果页面上显示所有字段。
  • @KrzysztofTrzos:这是 varchar(100)

标签: php mysql


【解决方案1】:
SELECT  *
FROM    product
WHERE   ProductCode = 'x' AND
        Type IN ('y', 'All Customers')
ORDER   BY FIELD(Type, 'y', 'All Customers')
LIMIT   1

xy 替换为演示中显示的所需值。

【讨论】:

    【解决方案2】:
    SELECT * 
    FROM Products 
    WHERE ProductCode = 'product_id' AND Type IN ('customer_id', 'All Customers') 
    ORDER BY Type 
    LIMIT 1
    

    【讨论】:

      【解决方案3】:
      SELECT * FROM 
         (SELECT * FROM product
            WHERE `Type` = '155000' and ProductCode = '56622000'
            LIMIT 1
          UNION
          SELECT * FROM product
            WHERE `Type` = 'all customers' and ProductCode = '56622000'
            LIMIT 1) a
      LIMIT 1;
      

      感谢@491243 提供小提琴代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-14
        • 2018-02-14
        • 1970-01-01
        相关资源
        最近更新 更多