MySQL的between的边界问题


=Start=

缘由:

之前在写一个Web应用的时候碰到过类似的问题,当时也不清楚between的边界到底是个什么情况,就用「>/>=/</<=」替换了。今天又碰到了,索性彻底了解一下。

正文:

参考解答:

官网上的说明:

expr BETWEEN min AND max

If expr is greater than or equal to min and expr is less than or equal to maxBETWEEN returns 1, otherwise it returns 0. This is equivalent to the expression (min <=expr AND expr <= max) if all the arguments are of the same type. Otherwise type conversion takes place according to the rules described in Section 13.2, “Type Conversion in Expression Evaluation”, but applied to all the three arguments.

即,MySQL的between是包含两边的边界值的,等价于「min <=expr AND expr <= max」。

另外一个「not between」:

expr NOT BETWEEN min AND max

This is the same as NOT (expr BETWEEN min AND max).

所以,「not between」是对「between」的取反,则不包含边界值,等价于「expr < min OR expr > max」。

参考链接:

=END=

, ,

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注