MySQL查看版本:

status;

或者

 select version(); //select @@version

MySQL昨天, 一周前 ,一月前 ,一年前的数据 这里主要用到了  DATE_SUB(date,INTERVAL expr unit)

select DATE_SUB(NOW(),INTERVAL  DAY);
select DATE_SUB(NOW(),INTERVAL WEEK);
select DATE_SUB(NOW(),INTERVAL MONTH);
select DATE_SUB(NOW(),INTERVAL YEAR);

MySQL增加日期  DATE_ADD(date,INTERVAL expr unit)

select DATE_ADD(date,INTERVAL expr unit)

MySQL格式化日期  DATE_FORMAT(date,format)

SELECT DATE_FORMAT(NOW(),'%Y-%m');
SELECT DATE_FORMAT(NOW(),'%Y')
%S, %s 两位数字形式的秒( 00,01, ..., 59)
%I, %i 两位数字形式的分( 00,01, ..., 59)
%H 两位数字形式的小时,24 小时(00,01, ..., 23)
%h 两位数字形式的小时,12 小时(01,02, ..., 12)
%k 数字形式的小时,24 小时(0,1, ..., 23)
%l 数字形式的小时,12 小时(1, 2, ..., 12)
%T 24 小时的时间形式(hh:mm:ss)
%r 12 小时的时间形式(hh:mm:ss AM 或hh:mm:ss PM)
%p AM或PM
%W 一周中每一天的名称(Sunday, Monday, ..., Saturday)
%a 一周中每一天名称的缩写(Sun, Mon, ..., Sat)
%d 两位数字表示月中的天数(00, 01,..., 31)
%e 数字形式表示月中的天数(1, 2, ..., 31)
%D 英文后缀表示月中的天数(1st, 2nd, 3rd,...)
%w 以数字形式表示周中的天数( 0 = Sunday, 1=Monday, ..., 6=Saturday)
%j 以三位数字表示年中的天数( 001, 002, ..., 366)
%U 周(0, 1, 52),其中Sunday 为周中的第一天
%u 周(0, 1, 52),其中Monday 为周中的第一天
%M 月名(January, February, ..., December)
%b 缩写的月名( January, February,...., December)
%m 两位数字表示的月份(01, 02, ..., 12)
%c 数字表示的月份(1, 2, ...., 12)
%Y 四位数字表示的年份
%y 两位数字表示的年份
%% 直接值“%”

MySQL类型转换 CAST 和 CONVERT

MySQL 的CAST()和CONVERT()函数可用来获取一个类型的值,并产生另一个类型的值

select CAST(expr AS type)
select CONVERT(expr,type)

类型必须是以下类型:

可用的类型: 
二进制,同带binary前缀的效果 : BINARY
字符型,可带参数 : CHAR()
日期 : DATE
时间: TIME
日期时间型 : DATETIME
浮点数 : DECIMAL
整数 : SIGNED
无符号整数 : UNSIGNED

查询重复数据

select  type,raw,auth ,report_type from category_shop_auth GROUP BY raw,auth,report_type HAVING COUNT()>

将一个表的数据查询出来导入另一个表

insert into  no_error.category_shop_auth(type,raw,auth,report_type) select type,raw,auth,report_type from mapping.category_shop_auth ORDER BY report_type,type

截取字符串

1、从左开始截取字符串 
left(str, length) 
说明:left(被截取字段,截取长度) 
例:select left(title,10) as tran_date from mytable
2、从右开始截取字符串 
right(str, length) 
说明:right(被截取字段,截取长度) 
例:select right(title,10) as tran_date from mytable
3、截取字符串 
substring(str, 'target') 
substring(str, 'target', length) 
说明:substring(被截取字段,从第几位开始截取) 
substring(被截取字段,从第几位开始截取,截取长度) 
例:select substring(str,10) from mytable
select substring(str,8,10) from mytable
(注:如果位数是负数 如-5 则是从后倒数位数,到字符串结束或截取的长度)

年月转日期

需要用concat做字符串拼接,不然纯数字会最加减的

select STR_TO_DATE(CONCAT('2018-01','-01'),'%Y-%m-%d') from dual

 MySql查看每个表占用空间大小

以吉比特为单位

use information_schema;
select concat(round(sum(DATA_LENGTH///),),'G') from tables where table_schema='数据库名' AND table_name='表名'

以兆比特为单位

use information_schema;
select concat(round(sum(DATA_LENGTH//),),'M') from tables where table_schema='数据库名' AND table_name='表名'

查询数据库里面的空表

sUSE information_schema;

-- Mysql 一个数据库所有有数据的表
SELECT table_schema,table_name,table_rows FROM TABLES WHERE table_rows != 0 ORDER BY table_rows DESC;
-- Mysql 一个数据库中所有为空的表
SELECT table_schema,table_name,table_rows FROM TABLES WHERE table_rows = 0 ORDER BY table_name ;
05-13 15:08
查看更多