简体   繁体   English

SQL返回始终为false的条件的结果

[英]SQL returns result for an always false condition

I'm confused on why the following SQL sentence returns one row: 我很困惑为什么以下SQL语句返回一行:

#MySQL
select max(1) from dual where 0 = 1

#SQLite
select max(1) where 0 = 1 

I'm really confused about this result. 我对这个结果感到很困惑。 Can anyone please shed some light over this behavior? 任何人都可以对这种行为有所了解吗?

Thanks in advance! 提前致谢!

An aggregate like max without a group by always returns one row. 像一个聚集max没有group by总是返回一行。 If the underlying rowset it empty, it will return a single row with null . 如果底层行集为空,则返回一行为null Otherwise, it returns the maximum of the column you specified. 否则,它返回您指定的列的最大值。

The where clause affects which rows the maximum is calculated for. where子句影响计算最大值的行。 It does not change the fact that the query has to return a maximum. 它不会改变查询必须返回最大值的事实。

因为MAX是标量函数,所以它需要返回结果,而不是表结果查询。

It its returning the max value (or NULL in the case of no matching rows), so a single row returned seems logical 它返回最大值(或者在没有匹配行的情况下为NULL),因此返回的单行似乎是合乎逻辑的

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_max http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_max

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

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