我试图显示客户从'Tucson'购买的产品的平均价格,但是这个查询返回null
,即使有两个客户已经从Tuscon下订单。
select AVG(product_price) from product where product_id in
(select product_id from orderline where order_id in
(select order_id from ordertable where cust_id in
(Select cust_id from customer where city = 'Tuscon')))
答案 0 :(得分:0)
SELECT AVG(p.product_price)
FROM ( SELECT l.product_id
FROM orderline l
JOIN ordertable o
ON o.order_id = l.order_id
JOIN customer c
ON c.cust_id = o.cust_id
WHERE c.city = 'Tuscon'
GROUP BY l.product_id
) q
JOIN product p
ON p.product_id = l.product_id
不是可识别的关键字。 MySQL解析器不知道如何处理它,因此它会引发关于"无效语法的错误"。
考虑使用连接操作代替嵌套的IN子查询。如果我们得到保证:
然后我们可以得到一个等价的结果集,即订购的不同产品的平均价格......
SELECT AVG(p.product_price)
FROM product p
JOIN orderline l
ON l.product_id = p.product_id
JOIN ordertable o
ON o.order_id = l.order_id
JOIN customer c
ON c.cust_id = o.cust_id
WHERE c.city = 'Tuscon'
如果我们想要"平均价格"订购的所有产品(不同的结果,平均值考虑到产品订购的次数......然后我们可以使用这样的查询:
{{1}}
答案 1 :(得分:0)
您在查询中使用SELECT i
FROM
(
select
i0.i
+i1.i*2
+i2.i*4
+i3.i*8
+i4.i*16
+i5.i*32
+i6.i*64
+i7.i*128
+i8.i*256
+i9.i*512
as i
from
(select 0 as i union select 1) as i0
cross join (select 0 as i union select 1) as i1
cross join (select 0 as i union select 1) as i2
cross join (select 0 as i union select 1) as i3
cross join (select 0 as i union select 1) as i4
cross join (select 0 as i union select 1) as i5
cross join (select 0 as i union select 1) as i6
cross join (select 0 as i union select 1) as i7
cross join (select 0 as i union select 1) as i8
cross join (select 0 as i union select 1) as i9
) Z
WHERE i>=1 AND i<=1000;
代替fom
:from
这应该是select order_id fom ordertable where cust_id in