sql >> Database teknologi >  >> RDS >> Mysql

Hvordan får jeg den maksimale værdi for et heltal i SQL?

I Mysql er der et billigt trick til at gøre dette:

mysql> select ~0;
+----------------------+
| ~0                   |
+----------------------+
| 18446744073709551615 |
+----------------------+

tilden er den bitvise negation. Den resulterende værdi er en bigint. Se:http://dev.mysql .com/doc/refman/5.1/da/bit-functions.html#operator_bitwise-invert

For de andre heltalsvarianter kan du bruge den rigtige bitshift-operator >> sådan:

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
\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
1 row in set (0.00 sec)


  1. Vedligeholdelse af orden i MySQL IN-forespørgsel

  2. MySQL COUNT() på tværs af flere kolonner

  3. SQL - Call Stored Procedure for hver post

  4. T-SQL:søger efter e-mail-format