我很早就想整理一波常用sql,这不就搞起来了。
先执行初始化sql,后面的就可以在这个数据表上面练习了,拿来即用。
我后面会慢慢完善这个大一统的sql集合的,感兴趣的话可以持续关注呀~
/* 目录数据准备user表相关选择数据库设计表结构索引相关增删改查条件查询时间查询连接查询存储过程查询数据库大小设置变量系统变量时间函数其它函数随机数**/-- 数据准备----------------------------------------CREATE TABLE `t_table_info` (`id` int(11) NOT null,`name` varchar(255) DEFAULT null COMMENT '姓名',`age` int(11) DEFAULT null COMMENT '年龄',`sex` varchar(255) DEFAULT null COMMENT '性别',`job` varchar(255) DEFAULT null COMMENT '职业',`json_set` varchar(100) DEFAULT null COMMENT 'json集合',`create_time` datetime DEFAULT null COMMENT '创建时间',`update_time` datetime DEFAULT null ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',PRIMARY KEY (`id`) USING BTREE,UNIQUE KEY `index_id` (`id`),KEY `index_name` (`name`),KEY `index_age_sex` (`age`,`sex`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMICINSERT INTO dbname.t_table_info (id, name, age, sex, job, json_set, create_time, update_time) VALUES(1, '张三', 1, '男', '律师', '{"uuid":"123"}', '2021-10-27 23:28:35', now());INSERT INTO dbname.t_table_info (id, name, age, sex, job, json_set, create_time, update_time) VALUES(2, '李四', 2, '男', '律师', '{"uuid":"123"}', '2021-10-13 23:28:38', now());INSERT INTO dbname.t_table_info (id, name, age, sex, job, json_set, create_time, update_time) VALUES(3, '王五', 3, '男', '医生', '{"uuid":"123"}', '2021-10-20 23:30:56', now());INSERT INTO dbname.t_table_info (id, name, age, sex, job, json_set, create_time, update_time) VALUES(4, '赵六', 4, '女', '医生', '{"uuid":"456"}', '2021-10-27 23:31:28', now());INSERT INTO dbname.t_table_info (id, name, age, sex, job, json_set, create_time, update_time) VALUES(5, '嘎嘎大王', 5, '女', '教师', '{"uuid":"456"}', '2021-11-21 21:58:17', now());INSERT INTO dbname.t_table_info (id, name, age, sex, job, json_set, create_time, update_time) VALUES(6, '呱呱大王', 6, '女', '教师', '{"uuid":"456"}', '2021-11-21 21:59:32', now());-- user表相关------------------------------------------ 查看用户表select * from mysql.`user`;-- 创建用户create user student identified by 'student';-- 修改用户名称update mysql.`user` set user = 'student_v2' where user ='student' and Host = 'localhost';-- 查看用户权限show grants for 'student'@'%';-- 选择数据库------------------------------------------ 选择数据库use dbname;-- 查看表show tables;-- 刷新权限变更flush privileges-- 设计表结构----------------------------------------select * from dbname.`t_table_info`;-- 查看表结构desc dbname.`t_table_info`;-- 添加列alter table dbname.`t_table_info` add new_col varchar(255) null comment '新增列';-- 修改列alter table dbname.`t_table_info` change new_col new_col_v2 varchar(50);-- 删除列alter table dbname.`t_table_info` drop column new_col_v2;-- 复制表结构create table dbname.`t_table_info_v2` like dbname.`t_table_info`;-- 复制表数据insert into dbname.`t_table_info_v2` select * from dbname.`t_table_info`;-- 重命名表rename table dbname.`t_table_info_v2` to dbname.`t_table_info_v3`;-- 删除表-- drop table dbname.`t_table_info_v3`;-- 清空表-- truncate table dbname.`t_table_info_v3`;-- 索引相关------------------------------------------ 查看索引show index from dbname.`t_table_info`;-- 创建唯一索引create unique index index_id on dbname.`t_table_info` (id);-- 创建普通索引,允许重复值,1个索引create index index_name on dbname.`t_table_info` (name);-- 创建普通索引,允许重复值,2个索引create index index_age_sex on dbname.`t_table_info` (age, sex);-- 删除索引drop index index_name on dbname.`t_table_info`;-- 增删改查------------------------------------------ 插入数据insert into dbname.`t_table_info` (id, name, age, sex, job, json_set, create_time, update_time, new_col) values(floor(1 + (rand() * 100)), '呱呱大王', 6, '女', '教师', '{"uuid":"456"}', now(), now(), null);-- 删除数据delete from dbname.`t_table_info` where id in ('6');-- 修改数据update dbname.`t_table_info` set name='呱呱大王', age=6, sex='女', job='教师', json_set='{"uuid":"456"}', create_time='2021-11-21 21:59:32', update_time=now() where id in ('6');-- 查询数据select * from dbname.`t_table_info`;-- 查询json:json_unquote() 去除双银行; json_extract() 提取json的keyselect json_unquote(json_extract(json_set,'$.uuid')) from dbname.`t_table_info`;-- 批量删除表:将结果复制出来并执行select concat('drop table if exists ', table_name, ';') from information_schema.tables where table_schema = 'dbname';-- 条件查询----------------------------------------select * from dbname.`t_table_info`;-- 选取指定的条数数据select * from dbname.`t_table_info` limit 5;-- 查询最新一条数据select * from dbname.`t_table_info` order by update_time desc limit 1;-- 升降序排序select * from dbname.`t_table_info` order by update_time desc, id desc;-- in查询select * from dbname.`t_table_info` where id in ('1','5');-- and和or查询select * from dbname.`t_table_info` where job in ('律师') and (id in ('1') or age in ('5'));-- between筛选某个范围内的值select * from dbname.`t_table_info` where age between '1' and '5';-- 模糊查询select * from dbname.`t_table_info` where name like '%大王%';-- 多条件模糊查询select * from dbname.`t_table_info` where name like '%大王' or job like '%律师%';-- 分组查询数据select job, count(0) as '重复次数' from dbname.`t_table_info` group by job;-- 查询重复数据并统计个数select job, count(job) as '重复次数' from dbname.`t_table_info` group by job having count(job)>1;-- 时间查询----------------------------------------select * from dbname.`t_table_info`;-- 查询今天的数据select * from dbname.`t_table_info` where to_days(create_time) = to_days(now());-- 查询昨天的数据select * from dbname.`t_table_info` where datediff(create_time, now())=-1;-- 查询最近7天的数据select * from dbname.`t_table_info` where date_sub(curdate(), interval 7 day) <= date(create_time);-- 查询本月的数据select * from dbname.`t_table_info` where date_format(create_time, '%Y%m') = date_format(curdate(), '%Y%m');-- 查询上月数据select * from dbname.`t_table_info` where period_diff(date_format(now(),'%Y%m'), date_format(create_time, '%Y%m'))=1;-- 连接查询----------------------------------------select * from dbname.`t_table_info`;-- 内连接-1:内连接查询操作列出与连接条件匹配的数据行select * from t_table_info a, t_table_info_v2 b where a.id = b.id;-- 内连接-2:内连接查询操作列出与连接条件匹配的数据行select * from t_table_info a inner join t_table_info_v2 b on a.id = b.id;-- 外连接-左连接:以左表为基准,右表的列为NULLselect * from t_table_info a left join t_table_info_v2 b on a.id = b.id;-- 外连接-右连接:以右表为基准,左表的列为NULLselect * from t_table_info a right join t_table_info_v2 b on a.id = b.id;-- 交叉连接:交叉联接返回左表中的所有行,左表中的每一行与右表中的所有行组合。交叉联接也称作笛卡尔积。select * from t_table_info a cross join t_table_info_v2 as b on a.id = b.id;-- 存储过程------------------------------------------ 简单查询 --select * from dbname.`t_table_info`;drop procedure if exists get_info;create procedure get_info()beginselect * from dbname.`t_table_info`;end;call get_info();-- 批量插入数据----------------------------------------select * from dbname.`t_table_info`;drop procedure if exists batch_data;create procedure batch_data()begindeclare i int;set i=7;while(i<=8) doinsert into dbname.`t_table_info` (id, name, age, sex, job, json_set, create_time, update_time) values(i, CONCAT('name',id), 1, '男', '律师', '{"uuid":"123"}', now(), now());set i=i+1;end while;end;call batch_data();-- 查询数据库表大小----------------------------------------select * from dbname.`t_table_info`;-- 查询所有数据库的表大小select table_name, concat(truncate(data_length/1024/1024,2),' mb') as data_size,concat(truncate(index_length/1024/1024,2),' mb') as index_sizefrom information_schema.tablesgroup by table_nameorder by data_length desc;-- 查询某个数据库的表大小select table_name, concat(truncate(data_length/1024/1024,2),' mb') as data_size,concat(truncate(index_length/1024/1024,2),' mb') as index_sizefrom information_schema.tables where table_schema = 'dbname'group by table_nameorder by data_length desc;-- 设置变量----------------------------------------select * from dbname.`t_table_info`;-- set设置变量set @job = '教师';set @age = (select age from dbname.`t_table_info` where id in ('6'));select * from dbname.`t_table_info` where job in (@job) and age in (@age);-- select设置变量select @job := '教师';select * from dbname.`t_table_info` where job in (@job);-- 查询结果并赋值select age, job into @age, @job from dbname.`t_table_info` where id in ('6');select * from dbname.`t_table_info` where age in (@age) and job in (@job);-- 系统变量------------------------------------------ 查看所有的会话变量show session variables;-- 查看所有的全局变量show global variables;-- 查看数据库隔离级别show variables like '%isolation';select @@global.tx_isolation;-- 查看MySQL版本show variables like 'version%';-- 查看端口、目录、数据存放目录、服务器idselect @@port, @@basedir, @@datadir, @@server_id;-- 时间函数------------------------------------------ 获取当前日期select date(curdate());-- 获取当前月份select month(curdate());-- 获取本月第一天select date_add(curdate(), interval-day(curdate())+1 day);-- 获取本月最后一天select last_day(curdate());-- 获取本月天数select day(last_day(curdate()));-- 获取当前年份select year(curdate());-- 其它函数------------------------------------------ concat:在字段值前加上前缀aaa-update dbname.`t_table_info` set name = concat('aaa-',name) where name in ('张三','李四');-- replace:去掉字段值的某一部分update dbname.`t_table_info` set name=replace(name,'aaa-','') where name in ('aaa-张三','aaa-李四');-- case举例selectname,(case when name = '张三' then '张三呀'when name = '李四' then '李四呀'when name is null then '空值'else '其它情况' end) as 备注from dbname.`t_table_info`;-- 随机数------------------------------------------ 生成3位随机数select ceiling(rand()*900+100);-- 生成4位随机数select ceiling(rand()*9000+100);-- 生成100以内的随机数select floor(1 + (rand() * 100));
文章转载自嘎嘎软件测试,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




