今天接到一個朋友電話說是覺的數據庫被別人更改了,出現數據不對的問題 。經過很久的排查是數據類型溢出了(發生問題的版本是MySQL 5.1)。後來通過給朋友那邊把MySQL 5.1升級到MySQL 5.5去解決這個問題。 這也讓我有興趣去了解一下MySQL不同版本數據類型溢出的處理機制。
先看一下MySQL支持的整型數及大小,存儲空間:
pe
Storage
Minimum Value
Maximum Value
存儲大小
(Bytes)
(Signed/Unsigned)
(Signed/Unsigned)
byte
TINYINT
1
-128
127
1 byte
0
255
SMALLINT
2
-32768
32767
2 bytes
0
65535
MEDIUMINT
3
-8388608
8388607
3 bytes
0
16777215
INT
4
-2147483648
2147483647
4 bytes
0
4294967295
BIGINT
8
-9223372036854775808
9223372036854775807
8 bytes
0
18446744073709551615
另外請記著mysql的數據處理會轉成bigint處理,所以這裡就用bigint幾個測試:
復制代碼 代碼如下:
SELECT CAST(0 AS UNSIGNED) - 1;
SELECT 9223372036854775807 + 1;
MySQL 5.1 下:
復制代碼 代碼如下:
mysql> SELECT CAST(0 AS UNSIGNED) - 1;
+-------------------------+
| CAST(0 AS UNSIGNED) - 1 |
+-------------------------+
| 18446744073709551615 |
+-------------------------+
1 row in set (0.01 sec)
mysql> SELECT 9223372036854775807 + 1;
+-------------------------+
| 9223372036854775807 + 1 |
+-------------------------+
| -9223372036854775808 |
+-------------------------+
1 row in set (0.01 sec)
MySQL 5.5, 5.6, 5.7下:
復制代碼 代碼如下:
mysql> SELECT CAST(0 AS UNSIGNED) - 1;
ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in '(cast(0 as unsigned) - 1)'
mysql>
mysql>
mysql>
mysql> SELECT 9223372036854775807 + 1;
ERROR 1690 (22003): BIGINT value is out of range in '(9223372036854775807 + 1)'
所在處理這類數據是一定要小心溢出(如早期有做弊沖Q幣就是利用這個方法處理)
這個問題有可能會出現積分消息,積分相加, 或是一些錢相關的業務中出現, 主庫5.1 ,從庫MySQL 5.5情況也會出現不同步的問題。
建議:這類業務系統盡可能的升級到MySQL 5.5後版本
更多詳情參考: http://dev.mysql.com/doc/refman/5.7/en/out-of-range-and-overflow.html
不知道你是oracle還是sql server?這個是個自連接問題,先要排序,標行號,再同表的上下行相比,所以是自連接
orcale:
select a.vseq,a.declaredate as declaredate1,b.declaredate as declaredate2
from
(select vseq,declaredate,rownum as row from mac505 order by vseq,declaredate)a,
(select vseq,declaredate,rownum as row from mac505 order by vseq,declaredate)b
where a.vseq=b.vseq and a.row+1=b.row and a.declaredate+1000<b.declaredate
sql server由於只有2005以上版本才有row_number()函數,所以如下腳本只能用在2005以上版本中
select a.vseq,a.declaredate as declaredate1,b.declaredate as declaredate2
from
(select vseq,declaredate,row_number() orver (order by vseq,declaredate) as row from mac505 )a,
(select vseq,declaredate,row_number() orver (order by vseq,declaredate) as row from mac505 )b
where a.vseq=b.vseq and a.row+1=b.row and a.declaredate+1000<b.declaredate
100000000000000表示是負數,所以答案要減1再取反。
011111111111111 //-1
100000000000000 //取反
最後將這個數變成十進制,然後加上負號。