【问题标题】:SELECT MAX with WHERE conditionSELECT MAX 与 WHERE 条件
【发布时间】:2018-03-28 13:53:14
【问题描述】:

我正在尝试执行以下 sql 查询

SELECT MAX(amount) AS LargestPrice FROM au_bids WHERE product = 73

我不能这样做,因为 WHERE 条件不起作用,它给了我整个表中最大数字的值。

我做错了什么?

【问题讨论】:

  • 一条错误消息将帮助我们帮助您..
  • 在我看来一切都很好
  • 请贴出你执行语句的数据(au_bids的示例内容),以及你得到的结果。
  • 当你选择不带 max 时 where 过滤器会起作用吗?
  • 产品 73 是否也恰好是最贵的?

标签: mysql sql


【解决方案1】:

您的查询是正确的

SELECT MAX(amount) AS LargestPrice FROM au_bids WHERE product = 73

查看现场演示

http://sqlfiddle.com/#!9/11bbf1/2

【讨论】:

  • 谢谢朋友。我很确定它也是正确的,但仍然 WHERE 条件不起作用。也许MYSQL版本有问题?感谢您的回复。
  • 欢迎您的回答,您可以接受@SerginistO
【解决方案2】:

您的查询是正确的

mysql> select * from Customers;
+-------------+---------------+
| customer_id | customer_name |
+-------------+---------------+
|           1 | Arun          |
|           2 | kumar         |
|           4 | bbbbb         |
|           5 | mmmmm         |
|           6 | kkkk          |
|           3 | eeeeee        |
+-------------+---------------+
6 rows in set (0.00 sec)

mysql> select max(customer_id) from Customers where customer_id=2;
+------------------+
| max(customer_id) |
+------------------+
|                2 |
+------------------+
1 row in set (0.00 sec)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-20
  • 2017-09-27
  • 2012-08-14
  • 2013-04-08
  • 1970-01-01
相关资源
最近更新 更多