首页 > 解决方案 > 当我试图找到平均值时出现错误

问题描述

SELECT AVG(product.product_price) 
  from product
     , customer
     , orderline
     , orders 
 WHERE product.product_id = orderline.product_id 
   AND orderline.order_id = orders.order_id 
   and = orders.cust_id = customer.cust_id 
   AND customer.city LIKE 'Tuscon'
 group 
    by product.name

我正在尝试查找客户在图森订购的产品的平均价格

标签: mysqlsqlinner-joinaveragewhere-clause

解决方案


我在第 5 行看到了一个语法问题。另外,不需要将 where 子句放在大括号中。此外,您尝试使用 JOIN 而不是“,”。

SELECT product.name, AVG(product.product_price) 
from product 
    inner join orderline on product.product_id = orderline.product_id
    inner join orders on orderline.order_id = orders.order_id 
    inner join  customer on orders.cust_id = customer.cust_id
WHERE  customer.city LIKE 'Tuscon'
group by product.name

推荐阅读