如何用季节表得到最终成本产品?
我有这个SQL模式。
http://sqlfiddle.com/#!9/79bb30/4/0
我用这个SQL

SELECT *,
       COALESCE(t1.cost, t1.base_price, `ec_products`.`price`) AS final_cost
FROM `ec_products`
LEFT JOIN
  (SELECT ec_season.cost,
          ec_season.base_price,
          ec_season.product_id
   FROM ec_season
   WHERE Duration =
       (SELECT MAX(Duration)
        FROM ec_season
        WHERE `Duration` <= 4 ) ) AS t1 ON `t1`.`product_id` = `ec_products`.`id`

但是,这个SQL查询的产品id为1。它报错价30.3英镑。
但必须给这个价格40.00。
因为MAX(Duration)你能帮我做这个吗?
我也试过这个
SELECT ec_season.cost,
              ec_season.base_price,
              ec_season.product_id
       FROM ec_season
       WHERE Duration =
           (SELECT ec_season.product_id ,  MAX(ec_season.Duration)
FROM ec_season WHERE `Duration` <= 4 GROUP BY ec_season.product_id)

但它给了我这个错误
1241-操作数应包含1列
季节表示例
mysql - 如何使用季节表获取最终成本产品-LMLPHP

最佳答案

问题在于:

   WHERE Duration = (SELECT ec_season.product_id ,
                            MAX(ec_season.Duration)
                     FROM ec_season
                     WHERE `Duration` <= 4
                     GROUP BY ec_season.product_id
                    )

=后面应该跟一个标量子查询。标量子查询返回一列(且不超过一行)。
你大概打算:
 FROM ec_season s
 WHERE s.Duration = (SELECT MAX(s2.Duration)
                     FROM ec_season s2
                     WHERE s2.Duration <= 4 AND
                           s2.product_id = s.product_id
                    )

关于mysql - 如何使用季节表获取最终成本产品,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52645522/

10-11 17:34