简体   繁体   English

带有 WHERE 条件的 MySQL MAX()

[英]MySQL MAX() with WHERE condition

I don't know if this is possible, but I want to filter the records available for the MAX() function.我不知道这是否可行,但我想过滤可用于 MAX() 函数的记录。

For example something like this:例如这样的事情:

SELECT COUNT(1), MAX(age WHERE person.date_of_death IS NULL) FROM person

I still want to count all the records, but only want to get the MAX age where date_of_death is null.我仍然想计算所有记录,但只想获取 date_of_death 为空的 MAX 年龄。 Obviously I could use a subquery, but the actual query is much more complex than this.显然我可以使用子查询,但实际查询比这复杂得多。

Thanks谢谢

Add case expression :添加case表达式:

SELECT COUNT(1), MAX(CASE WHEN p.date_of_death IS NULL THEN age END) 
FROM person p

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM