在MySql中有的时候需要获取整数的最大值——比如在LIMIT的时候:
-- 需要获取第5条记录到最后的所有记录
-- 不得不在最后输入一个最大值,这个值如果简单点我们希望是BIGINT的最大值
-- 不过,我们忘了BIGINT的最大值是?
SELECT * FROM table_name WHERE condition=cvalue LIMIT 5, 838882;
当忘掉整数的最大值的时候,我们最简单的方法是使用~0,让后通过位移来取得各个整数类型的对应最大值:
-- 通过~0来获取BIGINT的最大值(无符号)
-- 通过~0的右移位数来获取各个整数类型的最大值
-- \G 是让每一列占据一行显示
SELECT ~0 as max_bigint_unsigned
, ~0 >> 32 as max_int_unsigned
, ~0 >> 40 as max_mediumint_unsigned
, ~0 >> 48 as max_smallint_unsigned
, ~0 >> 56 as max_tinyint_unsigned
, ~0 >> 1 as max_bigint_signed
, ~0 >> 33 as max_int_signed
, ~0 >> 41 as max_mediumint_signed
, ~0 >> 49 as max_smallint_signed
, ~0 >> 57 as max_tinyint_signed
, 'This code came from internet resource -- MitchellChu' as extinfo
\G
*************************** 1. row ***************************
max_bigint_unsigned: 18446744073709551615
max_int_unsigned: 4294967295
max_mediumint_unsigned: 16777215
max_smallint_unsigned: 65535
max_tinyint_unsigned: 255
max_bigint_signed: 9223372036854775807
max_int_signed: 2147483647
max_mediumint_signed: 8388607
max_smallint_signed: 32767
max_tinyint_signed: 127
extinfo: This code came from internet resource -- MitchellChu
1 row in set (0.00 sec)
看完代码是不是有恍然大悟的感觉呢?