mysql 繁體 mysql日常整理

2021-10-17 12:55:01 字數 4230 閱讀 8126

mysql的基本資料型別裡幾個int如下:

型別         大小      範圍(有符號)      範圍(無符號)    用途

tinyint        1位元組 (-128,127) (0,255) 小整數值

smallint     2 位元組 (-32 768,32 767) (0,65 535) 大整數值

mediumint   3 位元組(-8 388 608,8 388 607) (0,16 777 215) 大整數值

int或integer 4 位元組 (-2 147 483 648,2 147 483 647) (0,4 294 967 295) 大整數值

bigint 8 位元組 (-9 233 372 036 854 775 808,9 223 372 036 854 775 807) (0,18 446 744 073 709 551 615) 極大整數值

整數型別       位元組       範圍(有符號)      範圍(無符號)          用途

tinyint        1位元組        (-128,127)         (0,255)            小整數值

smallint       2位元組     (-32 768,32 767)       (0,65 535)         大整數值

mediumint      3位元組    (-8 388 608,8 388 607) (0,16 777 215)      大整數值

int或integer   4位元組   (-2 147 483 648,2 147 483 647) (0,4 294 967 295) 大整數值

bigint         8位元組   (-9 233 372 036 854 775 808,9 223 372 036 854 775 807) (0,18 446 744 073 709 551 615) 極大整數值

float          4位元組   (-3.402 823 466 e+38,1.175 494 351 e-38),0,(1.175 494 351 e-38,3.402 823 466 351 e+38) 0,(1.175 494 351 e-38,3.402 823 466 e+38) 單精度浮點數值

double        8位元組 (1.797 693 134 862 315 7 e+308,2.225 073 858 507 201 4 e-308),0,(2.225 073 858 507 201 4 e-308,1.797 693 134 862 315 7 e+308) 0,(2.225 073 858 507 201 4 e-308,1.797 693 134 862 315 7 e+308) 雙精度浮點數值

decimal 對decimal(m,d) ,如果m>d,為m+2否則為d+2 依賴於m和d的值 依賴於m和d的值 小數值

date為需要處理的引數(該引數是unix 時間戳),可以是欄位名,也可以直接是unix 時間戳字串

後面的 '%y%m%d' 主要是將返回值格式化

例如:mysql>select from_unixtime( 1249488000, '%y%m%d' )

->20071120

mysql>select from_unixtime( 1249488000, '%y年%m月%d' )

->2023年11月20

unix_timestamp()是與之相對正好相反的時間函式

unix_timestamp(), unix_timestamp(date)

mysql日期和字元相互轉換方法

date_format(date,'%y-%m-%d') -------------->oracle中的to_char();

str_to_date(date,'%y-%m-%d') -------------->oracle中的to_date();

%y:代表4位的年份

%y:代表2為的年份

%m:代表月, 格式為(01……12)

%c:代表月, 格式為(1……12)

%d:代表月份中的天數,格式為(00……31)

%e:代表月份中的天數, 格式為(0……31)

%h:代表小時,格式為(00……23)

%k:代表 小時,格式為(0……23)

%h: 代表小時,格式為(01……12)

%i: 代表小時,格式為(01……12)

%l :代表小時,格式為(1……12)

%i: 代表分鐘, 格式為(00……59)

%r:代表 時間,格式為12 小時(hh:mm:ss [ap]m)

%t:代表 時間,格式為24 小時(hh:mm:ss)

%s:代表 秒,格式為(00……59)

%s:代表 秒,格式為(00……59)

日期比較示例:

select * from test_test1 where birth_daetime='2013/06/28 17:47:52'

select * from test_test1 where birth_day='2013/06/28'

select * from test_test1 where birth_day='2013-06-28'

select * from test_test1 where birth_daetime=str_to_date('2013-06-28 17:47:52', '%y-%m-%d %h:%i:%s')

select * from test_test1 where birth_day=str_to_date('2013/06/28', '%y/%m/%d')

select * from test_test1 where birth_day=str_to_date('2013-06-28 00:00:00', '%y-%m-%d %h:%i:%s')

select a.name, date_format(a.checktime,'%y-%m-%d'), min(a.checktime),

str_to_date(concat(date_format(a.checktime,'%y-%m-%d'),' 08:',floor(20 + (rand() * 30)),':00'),'%y-%m-%d %h:%i:%s'),

floor(20 + (rand() * 30))

from `inoutdata03.15` a

group by a.name, date_format(a.checktime,'%y-%m-%d')

mysql數值處理函式floor與round  在mysql中,當處理數值時,會用到數值處理函式,如有乙個float型數值2.13,你想只要整數2,那就需要下面的函式floor與round。

floor:函式只返回整數部分,小數部分捨棄。

round:函式四捨五入,大於0.5的部分進製,不到則捨棄。與floor不同。

rownum

set @x=0

select @x:=ifnull(@x,0)+1 as rownum,b.id,a.id,localtime from ya_disease a,ya_doctor b;

point

st_distance(point(113.327955,23.129717),point)*111195    座標point(113.327955,23.129717)與point的距離

mysql 5.6.1 加入了空間資料支援功能,新增了st_*相關函式,可以非常方便的計算兩個地理座標點的距離了。

如下例子:按我的座標計算周邊座標的距離並由近到遠排序

select name,st_distance(point(113.327955,23.129717),point)*111195 as distance,address from table1 where st_distance(point(113.327955,23.129717),point)*111195 < 100 order by distance asc limit 100

注意:其中point欄位型別是 point,其值可以通過以下方法寫入:

update table1 set point = point(113.123232,24.1324234)

st_distance 計算的結果單位是 度,需要乘111195(地球半徑6371000*pi/180) 是將值轉化為公尺

mysql5.7之前解析json字串

substring_index(

replace (

column_name, concat(substring_index(column_name, '"解析的欄位名":', 1),'"解析的欄位名":"'),''),'"',1

mysql的日常操作 mysql日常操作命令

1.mysql連線 埠要用大寫p,與密碼p加以區分 mysql h127.0.0.1 p3306 uroot p 2.檢視mysql的資料庫列表 show databases 3.使用某個庫 use 資料庫名 4.檢視表列表 show tables 5.檢視資料庫的建立sql show create...

mysql 字串中取整 MySQL取整

對乙個表取任意隨機數 select from tmp xf test where id select floor rand select max id from tmp xf test order by id limit 1 有條件性的取隨機數 select from tmp xf test whe...

mysql日常維護

檢視mysql執行程序 mysql show processlist 檢視innodb引擎運 況 mysql show engine innodb status g 檢視mysql引數情況 mysql show global variables like innodb 檢視mysql執行狀態 mys...