获取表创建语句
SHOW CREATE TABLE table
7天后
DATE_ADD(NOW(), INTERVAL 7 DAY);//2020-10-16 14:56:42
7天前
select DATE_SUB(NOW(), INTERVAL 7 DAY);//2020-09-23
select DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 7 DAY),'%Y-%m-%d %H:%i:%S');//2020-09-23 09:21:19
删除多个字段
alter table table_name
drop column `field_name`,
drop column `field_name`;
新增多个字段
alter table table_name
add column `field_name` VARCHAR(50) NULL DEFAULT NULL COMMENT '备注',
add column `field_name` VARCHAR(50) NULL DEFAULT NULL COMMENT '备注' after field_name;
获取某列数据为字符串
//某字段10行数据转字符串
GROUP_CONCAT(id) //1,2,3
//连接一个或者多个字符串
concat('11','22','33') // 112233
时间操作
//获取系统时间戳
unix_timestamp()
//获取当前时间
now()
//时间转时间戳
unix_timestamp('2018-01-15 09:45:16')
//时间戳转时间
from_unixtime(1515980716, '%Y-%m-%d %H:%i:%S')
获取要删除表的语句
SELECT concat('DROP TABLE IF EXISTS `', table_name, '`;')
FROM information_schema.tables
WHERE table_schema = 'db_name' AND Table_name LIKE '%table_name%';
某库的表总数
SELECT count(TABLE_NAME) FROM information_schema.TABLES WHERE TABLE_SCHEMA='db_name';
获取gourp by 后的总条数
select count(*) as total from (select count(column_name) from table_name group by column_name) as t
不重复字段的条数
... count(distinct column_name) ...
获取分组字段的所有信息
... GROUP_CONCAT(column_name) ... GROUP BY column_name