mysql 好用的sql语句

1.删除某个库里面全部的表 ,先在mysql库中执行: 

SELECT CONCAT('drop table ',table_name,';') FROM information_schema.`TABLES` WHERE table_schema='库名';

在到相应的库里面执行上句执行得到的结果。

2.MySql按周,按月,按日分组统计数据

select DATE_FORMAT(create_time,'%Y%u') weeks,count(caseid) count from tc_case group by weeks;  
select DATE_FORMAT(create_time,'%Y%m%d') days,count(caseid) count from tc_case group by days;  
select DATE_FORMAT(create_time,'%Y%m') months,count(caseid) count from tc_case group by months;

DATE_FORMAT(date,format) 

根据format字符串格式化date值。下列修饰符可以被用在format字符串中: 

%M 月名字(January……December) 
%W 星期名字(Sunday……Saturday) 
%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。) 
%Y 年, 数字, 4 位 
%y 年, 数字, 2 位 
%a 缩写的星期名字(Sun……Sat) 
%d 月份中的天数, 数字(00……31) 
%e 月份中的天数, 数字(0……31) 
%m 月, 数字(01……12) 
%c 月, 数字(1……12) 
%b 缩写的月份名字(Jan……Dec) 
%j 一年中的天数(001……366) 
%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) 
%p AM或PM 
%w 一个星期中的天数(0=Sunday ……6=Saturday ) 
%U 星期(0……52), 这里星期天是星期的第一天 
%u 星期(0……52), 这里星期一是星期的第一天 
%% 一个文字“%”。

3.将数据库拷贝到一个数据库中:

mysqldump sourcedb -u <USERNAME> -p<PASS> | mysql destdb -u <USERNAME> -p<PASS>

4. 查看数据库表占的空间大小

SELECT t.TABLE_SCHEMA,
       t.TABLE_NAME,
       t.TABLE_ROWS,
       t.DATA_LENGTH,
       t.INDEX_LENGTH,
       concat(round(t.DATA_FREE / 1024 / 1024, 2), 'M') AS datafree   
FROM information_schema.tables t
WHERE t.TABLE_SCHEMA = 'testdb' and t.table_name='orders'

select concat(round(sum(data_length/1024/1024/1024),2),'G') as data  FROM information_schema.tables where table_schema='testdb' ;

  

5.创建数据库 

CREATE DATABASE  `test` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

  

6.mysql慢日志查询

show variables like '%slow_query_log%';

show variables like 'long_query_time';

show variables like '%log_output%';

show global status like '%slow_queries%';

 mysql慢查询:https://www.cnblogs.com/1021lynn/p/5328495.html

7. 查看binlog相关的设置

show variables like 'log_%';

  

8.查看MySql数据库物理文件存放位置

show global variables like "%datadir%";

9.怎样把mysql中一个数据库的表复制到另一个数据库中的表  

CREATE TABLE mytbl_new LIKE production.mytbl;
INSERT mytbl_new SELECT * FROM production.mytbl;

第一个命令是创建新的数据表 mytbl_new ,并复制 mytbl 的数据表结构。
第二个命令是讲数据表 mytbl 中的数据复制到新表 mytbl_new 。
注:production.mytbl是指定要复制表的数据库名称为 production 。它是可选的。
假如没有production. ,MySQL数据库将会假设mytbl在当前操作的数据库。  

10.查看连接数

show variables like  'max_user_connections'; 
show variables like  'max_connections'; 
show processlist ;

show full processlist;

grant usage on *.* to root@'%' with max_user_connections 10000;

  

 

原文地址:https://www.cnblogs.com/nele/p/7092274.html