[英]Error in SQL statement using condition in having clause
I have a products
table with columns id
, price
, category
, and city
.我有一个包含id
、 price
、 category
和city
列的products
表。 I need to find the count of the cheapest veggies
in a Barcelona
city.我需要找到Barcelona
城市中最便宜veggies
的数量。
I tried this SQL query, but I get an error:我尝试了这个 SQL 查询,但出现错误:
SELECT COUNT( * )
FROM products
HAVING lower(category) = 'veggies' AND lower(city) = 'Barcelona'
AND price = (SELECT MIN(price)
FROM products
WHERE lower(category) = 'veggies' and lower(city) = 'Barcelona');
Can you please let me know where I am going wrong?你能告诉我我哪里出错了吗?
Look at the documentation of sql看sql的文档
You need a where not a having here, and it comes before the joins你需要一个 where not a have here,它出现在连接之前
SELECT COUNT( * )
FROM products pd
Where lower(category) = 'veggies' AND lower(city) = 'Barcelona'
AND price = (
SELECT MIN(price)
FROM products pd
WHERE lower(category) = 'veggies' and lower(city) = 'Barcelona'
);
The HAVING keyword is used when you're doing a group by, and need a where clause. HAVING 关键字在您执行 group by 时使用,并且需要一个 where 子句。
SELECT COUNT( * )
FROM products
WHERE city = 'Barcelona' and categories = 'veggies'
ORDER BY price asc
This will give you the cheapest veggies in Barcelona.这将为您提供巴塞罗那最便宜的蔬菜。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.