MySQL常用SQL整理

时间:2021-12-19 00:41:13

MySQL常用SQL整理

一、DDL

#创建数据库
CREATE DATABASE IF NOT EXISTS product DEFAULT CHARSET utf8 COLLATE utf8_general_ci; SET FOREIGN_KEY_CHECKS=0;
#创建表
DROP TABLE IF EXISTS `t_app`;
CREATE TABLE `t_app` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`create_date` datetime NOT NULL,
`is_delete` bit(1) DEFAULT b'0',
`re_order` int(11) DEFAULT '0',
`update_date` varchar(255) DEFAULT NULL,
`type_code` varchar(32) NOT NULL COMMENT '类型编码',
`type_name` varchar(32) NOT NULL COMMENT '类型名称',
`memo` TEXT COMMENT '备注信息',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; #取得空字符串
UPDATE t_app SET type_code=REPLACE(type_code,' ',''); #增加一个字段
ALTER TABLE t_app ADD COLUMN create_id INT(11) DEFAULT 0; ALTER TABLE t_app ADD COLUMN create_id INT(11) DEFAULT 0 AFTER id;
ALTER TABLE t_app ADD COLUMN create_id INT(11) DEFAULT 0 FIRST id; #修改表字段长度
ALTER TABLE t_app MODIFY COLUMN type_code VARCHAR(255); #PRIMARY KEY(主键索引)
mysql>ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` ) #UNIQUE(唯一索引)
mysql>ALTER TABLE `table_name` ADD UNIQUE (
`column`
)
#INDEX(普通索引)
mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column` ) #FULLTEXT(全文索引)
mysql>ALTER TABLE `table_name` ADD FULLTEXT (
`column`
) #多列索引
mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column1`, `column2`, `column3` ) #删除索引
ALTER TABLE table_name DROP INDEX index_name;

二、DML


INSERT INTO t_app(create_date,is_delete,re_order,update_date,type_code,type_name) VALUES(NOW(),0,0,now(),"10000",'TestAPP'); UPDATE t_app SET is_delete=b'1' WHERE id=1; DELETE FROM t_app WHERE id=1; #按周分组汇总
SELECT
WEEK(t.create_date,3) AS w,
WEEKOFYEAR(t.create_date) AS w1,
YEARWEEK(t.create_date,3) AS w2,
CONCAT('今天星期',(WEEKDAY(CURDATE())+1)) AS wd,
CONCAT(DATE_ADD('1900-01-01',INTERVAL FLOOR(DATEDIFF(t.create_date,'1900-01-01')/7)*7 DAY),'~',
DATE_ADD('1900-01-01',INTERVAL FLOOR(DATEDIFF(t.create_date,'1900-01-01')/7)*7+6 DAY)) AS `按周分组汇总`,
COUNT(DISTINCT imsi) AS amount
FROM t_pay_log t
WHERE t.create_date BETWEEN '2016-05-01' AND NOW()
GROUP BY FLOOR(DATEDIFF(t.create_date,'1900-01-01')/7); #行转列
SELECT DATE_FORMAT(t.create_date,'%Y-%m-%d') AS d,
SUM(IF(result_code='1030000',1,0)) AS `b.支付成功`,
SUM(IF(result_code IN ('1000000','1010002','1000007','1020004','9018'),1,0)) AS `c.代理IP问题`,
SUM(IF(result_code='22',1,0)) AS `e.鉴权失败`,
SUM(IF(result_code='2085',1,0)) AS `i.IP异常`,
SUM(IF(result_code IN ('13','14','9019'),1,0)) AS `k.网络连接失败`,
COUNT(0) AS `总计`
FROM test.c_pay_log t
WHERE 1=1
AND t.create_date BETWEEN '2016-01-01 00:00:00' AND '2016-01-31 23:59:59'
GROUP BY d; SELECT DATE_FORMAT(t.create_date,'%Y-%m-%d') AS d,
t.result_code,
t.result_msg,
CASE
t.result_code
WHEN '1030000' THEN 'b.支付成功'
WHEN '1000000' THEN 'c.代理IP问题'
WHEN '1010002' THEN 'c.代理IP问题'
WHEN '1000007' THEN 'c.代理IP问题'
WHEN '1020004' THEN 'c.代理IP问题'
WHEN '9018' THEN 'c.代理IP问题'
WHEN '22' THEN 'e.鉴权失败'
WHEN '2085' THEN 'i.IP异常'
WHEN '13' THEN 'k.网络连接失败'
WHEN '14' THEN 'k.网络连接失败'
ELSE 'z.其他问题'
END AS category,
COUNT(0) AS amount
FROM test.c_pay_log t
WHERE 1=1
AND t.create_date BETWEEN '2016-01-01 00:00:00' AND '2016-01-31 23:59:59'
GROUP BY d,t.result_code,t.result_msg
ORDER BY d ASC,category ASC; SELECT x.d,CONCAT('',x.sp_code) AS sp_code,CONCAT(x.sp_code,'(',x.sp_name,')') AS sp,x.appCode,CONCAT(x.dev_code,'(',x.dev_name,')') AS cp,
SUM(req) AS req, #请求数
SUM(req_su) AS req_su,#请求成功数
IFNULL(ROUND((SUM(req_su)/SUM(req))*100,2),0) AS req_su_rate, #请求成功率=请求成功数/请求数
SUM(pay) AS pay,#支付数
SUM(pay_su) AS pay_su,#支付成功数
SUM(pay_su_amount) AS pay_su_amount,#支付成功金额
IFNULL(ROUND((SUM(pay_su)/SUM(pay))*100,2),0) AS pay_su_rate,#支付成功率
IFNULL(ROUND((SUM(pay)/SUM(req_su))*100,2),0) AS sure_rate,#确认率=支付数/请求成功数
IFNULL(ROUND((SUM(pay_su)/SUM(req))*100,2),0) AS req_conv_rate #总体转化率=支付成功数/请求数
FROM
(
SELECT DATE_FORMAT(t.create_date,'%Y-%m-%d') AS d,sp.sp_code,sp.dev_name AS sp_name,dev.dev_code,dev.dev_name, t.appCode,
COUNT(0) AS req,0 AS req_su,0 AS pay,0 AS pay_su,0 AS pay_su_amount
FROM t_pay_log_re t,t_sp_channel spchannel,t_sp sp,t_developer dev
WHERE 1=1
AND t.mscCode=spchannel.ringtones_id AND spchannel.sp_id=sp.id AND t.developer_id=dev.id
AND t.create_date BETWEEN CONCAT(CURDATE(),' 00:00:00') AND NOW()
AND sp.sp_code='0101'
GROUP BY d,t.appCode UNION ALL SELECT DATE_FORMAT(t.create_date,'%Y-%m-%d') AS d,sp.sp_code,sp.dev_name AS sp_name,dev.dev_code,dev.dev_name,t.appCode,
0 AS req,COUNT(0) AS req_su,0 AS pay,0 AS pay_su,0 AS pay_su_amount
FROM t_pay_log_re t,t_sp_channel spchannel,t_sp sp,t_developer dev
WHERE 1=1
AND t.mscCode=spchannel.ringtones_id AND spchannel.sp_id=sp.id AND t.developer_id=dev.id
AND t.create_date BETWEEN CONCAT(CURDATE(),' 00:00:00') AND NOW()
AND sp.sp_code='0101'
AND t.result='success'
GROUP BY d,t.appCode UNION ALL SELECT DATE_FORMAT(t.create_date,'%Y-%m-%d') AS d,sp.sp_code,sp.dev_name AS sp_name,dev.dev_code,dev.dev_name,t.appCode,
0 AS req,0 AS req_su,COUNT(0) AS pay,0 AS pay_su,0 AS pay_su_amount
FROM t_pay_log t,t_sp_channel spchannel,t_sp sp,t_developer dev
WHERE 1=1
AND t.mscCode=spchannel.ringtones_id AND spchannel.sp_id=sp.id AND t.developer_id=dev.id
AND t.create_date BETWEEN CONCAT(CURDATE(),' 00:00:00') AND NOW()
AND sp.sp_code='0101'
GROUP BY d,t.appCode UNION ALL SELECT DATE_FORMAT(t.create_date,'%Y-%m-%d') AS d,sp.sp_code,sp.dev_name AS sp_name,dev.dev_code,dev.dev_name,t.appCode,
0 AS req,0 AS req_su,0 AS pay,COUNT(0) AS pay_su,SUM(t.cost) AS pay_su_amount
FROM t_pay_log t,t_sp_channel spchannel,t_sp sp,t_developer dev
WHERE 1=1
AND t.mscCode=spchannel.ringtones_id AND spchannel.sp_id=sp.id AND t.developer_id=dev.id
AND t.create_date BETWEEN CONCAT(CURDATE(),' 00:00:00') AND NOW()
AND sp.sp_code='0101'
AND t.result='success'
GROUP BY d,t.appCode
) x GROUP BY x.d,x.appCode
ORDER BY x.req DESC LIMIT 10 SELECT DISTINCT paylog.id AS book_id
FROM t_pay_log paylog
WHERE paylog.create_date BETWEEN DATE_SUB(NOW(),INTERVAL 1 HOUR) AND DATE_SUB(NOW(),INTERVAL 1 MINUTE); 查询表
SELECT TABLE_NAME FROM information_schema.TABLES t WHERE t.TABLE_SCHEMA='sdk';
或 show tables; desc t_app;

三、DCL

#导出表结构
mysqldump -h192.168.1.101 -uroot -p123456 -d test20160101 > test表结构_20160101.sql
mysqldump -h192.168.1.106 -uroot -p123456 testdb t_log --where=" appCode='200012' and create_date between '2001-07-01 00:00:00' and '2001-07-02 00:00:00'" > /data/t_log_0701.sql
更多mysqldump参考:
http://www.cnblogs.com/qq78292959/p/3637135.html http://www.javaranger.com/archives/1598 授权/改密
UPDATE user SET Password=PASSWORD('123456') where USER='root'; GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '123456' WITH GRANT OPTION;
FLUSH PRIVILEGES; GRANT ALL PRIVILEGES ON *.* TO 'root'@'112.87.45.22' IDENTIFIED BY '123456' WITH GRANT OPTION;
DELETE FROM mysql.user WHERE Host='112.87.45.22';
FLUSH PRIVILEGES; GRANT SELECT ON *.* TO 'readonly'@'192.168.1.%' IDENTIFIED BY '123456' WITH GRANT OPTION;
DELETE FROM mysql.user WHERE Host='112.87.45.105';
FLUSH PRIVILEGES; GRANT EXECUTE ON test.* TO 'jack'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; GRANT ALL PRIVILEGES ON *.* TO 'jack'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; DELETE FROM mysql.user WHERE Host='153.34.116.212';
FLUSH PRIVILEGES; 通过RENAME操作大表(创建索引,删除,备份等)
SHOW CREATE TABLE t_app;
CREATE TABLE t_app_backup;
RENAME TABLE t_app TO t_app_backup,t_app_new TO t_app;

四、综合


1.时间函数
http://www.w3school.com.cn/sql/sql_dates.asp (1)DATE_FORMAT
http://www.w3school.com.cn/sql/func_date_format.asp DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s') DATE_SUB(NOW(),INTERVAL 1 WEEK) SELECT FROM_UNIXTIME( 1249488000, '%Y%m%d' )
->20071120
mysql> SELECT UNIX_TIMESTAMP() ; (执行时的时间:2009-08-06 10:10:40)
->1249524739
mysql> SELECT UNIX_TIMESTAMP('2009-08-06') ;
->1249488000 #要得到正确的时间相减秒值,有以下3种方法:
1、time_to_sec(timediff(t2, t1)),
2、timestampdiff(second, t1, t2),
3、unix_timestamp(t2) -unix_timestamp(t1)

五、监控

 查看mysql数据库连接数、并发数相关信息
mysql> show status like 'Threads%';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_cached | 58 |
| Threads_connected | 57 | ###这个数值指的是打开的连接数
| Threads_created | 3676 |
| Threads_running | 4 | ###这个数值指的是激活的连接数,这个数值一般远低于connected数值
+-------------------+-------+ Threads_connected 跟show processlist结果相同,表示当前连接数。准确的来说,Threads_running是代表当前并发数 这是是查询数据库当前设置的最大连接数
mysql> show variables like '%max_connections%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 1000 |
+-----------------+-------+ 可以在/etc/my.cnf里面设置数据库的最大连接数
[mysqld]
max_connections = 1000 mysql 性能优化方向
http://www.cnblogs.com/AloneSword/p/3207697.html mysql>set profiling=1; 当前的连接数:
show status like '%Threads_connected%';
show status like '%Connections%'; 最大连接数:
show variables like '%max_connections%'; show global variables like '%timeout';