MySQL SUM() 带有 MIN() 的子查询

发布于 2024-12-16 12:23:42 字数 824 浏览 2 评论 0原文

首先,我想要完成的任务是:

获取 etp_product.priceoption_price 的总和。

SELECT 
  etp_product.product_id,
  etp_product.price,
  (SELECT MIN(price) AS FIELD_2 FROM etp_product_option_value pov WHERE pov.product_id = etp_product.product_id) AS option_price
FROM
  etp_product
GROUP BY
  etp_product.product_id,
  etp_product.price

我已经尝试过:

SELECT 
  etp_product.product_id,
  etp_product.price,
  (SELECT MIN(price) AS FIELD_2 FROM etp_product_option_value pov WHERE pov.product_id = etp_product.product_id) AS option_price,
SUM(etp_product.price + option_price) AS Total
FROM
  etp_product
GROUP BY
  etp_product.product_id,
  etp_product.price

但出现此错误:

“字段列表”中存在未知列“option_price”

知道如何完成吗?

First of all here what i'm trying to accomplish:

Get the sum of etp_product.price and option_price.

SELECT 
  etp_product.product_id,
  etp_product.price,
  (SELECT MIN(price) AS FIELD_2 FROM etp_product_option_value pov WHERE pov.product_id = etp_product.product_id) AS option_price
FROM
  etp_product
GROUP BY
  etp_product.product_id,
  etp_product.price

I've tried this:

SELECT 
  etp_product.product_id,
  etp_product.price,
  (SELECT MIN(price) AS FIELD_2 FROM etp_product_option_value pov WHERE pov.product_id = etp_product.product_id) AS option_price,
SUM(etp_product.price + option_price) AS Total
FROM
  etp_product
GROUP BY
  etp_product.product_id,
  etp_product.price

But get this error:

Unknown column 'option_price' in 'field list'

Any Idea how it could be done ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白昼 2024-12-23 12:23:42
SELECT 
  p.product_id,
  p.price,
  IFNULL(MIN(pv.price), 0) as option_price,
  (p.price + IFNULL(MIN(pv.price), 0)) as total
FROM etp_product p
JOIN etp_product_option_value pv ON WHERE pv.product_id = p.product_id
GROUP BY
  p.product_id,
  p.price
SELECT 
  p.product_id,
  p.price,
  IFNULL(MIN(pv.price), 0) as option_price,
  (p.price + IFNULL(MIN(pv.price), 0)) as total
FROM etp_product p
JOIN etp_product_option_value pv ON WHERE pv.product_id = p.product_id
GROUP BY
  p.product_id,
  p.price
断舍离 2024-12-23 12:23:42
SELECT 
etp_product.product_id,
  etp_product.price,
  (SELECT MIN(price) AS FIELD_2 FROM etp_product_option_value pov WHERE pov.product_id = etp_product.product_id) AS option_price,
 etp_product.price + (SELECT MIN(price) AS FIELD_3 FROM etp_product_option_value pov WHERE pov.product_id = etp_product.product_id) AS Total
FROM
  etp_product
GROUP BY
  etp_product.product_id,
  etp_product.price
SELECT 
etp_product.product_id,
  etp_product.price,
  (SELECT MIN(price) AS FIELD_2 FROM etp_product_option_value pov WHERE pov.product_id = etp_product.product_id) AS option_price,
 etp_product.price + (SELECT MIN(price) AS FIELD_3 FROM etp_product_option_value pov WHERE pov.product_id = etp_product.product_id) AS Total
FROM
  etp_product
GROUP BY
  etp_product.product_id,
  etp_product.price
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文