42人参与 • 2026-07-19 • MsSqlserver
查询所有数据:
select * from 表名;
查询特定列:
select 列名1, 列名2 from 表名;
条件查询:
select * from 表名 where 条件;
模糊查询:
select * from 表名 where 列名 like '模式%';
排序查询:
select * from 表名 order by 列名 asc|desc;
限制返回行数:
select * from 表名 limit 10;
去重查询:
select distinct 列名 from 表名;
聚合函数 - 计数:
select count(*) from 表名;
分组查询:
select 列名, count(*) from 表名 group by 列名;
条件分组:
select 列名, count(*) from 表名 group by 列名 having count(*) > 1;
计算总和:
select sum(列名) from 表名;
计算平均值:
select avg(列名) from 表名;
计算最大值:
select max(列名) from 表名;
计算最小值:
select min(列名) from 表名;
插入数据:
insert into 表名 (列名1, 列名2) values (值1, 值2);
批量插入数据:
insert into 表名 (列名1, 列名2) values (值1, 值2), (值3, 值4);
更新数据:
update 表名 set 列名 = 新值 where 条件;
删除数据:
delete from 表名 where 条件;
创建表:
create table 表名 (列名1 数据类型, 列名2 数据类型);
删除表:
drop table 表名;
修改表结构:
alter table 表名 add 列名 数据类型;
删除表中的列:
alter table 表名 drop column 列名;
重命名表:
alter table 旧表名 rename to 新表名;
创建索引:
create index 索引名 on 表名 (列名);
删除索引:
drop index 索引名;
创建视图:
create view 视图名 as select * from 表名;
删除视图:
drop view 视图名;
内连接:
select * from 表1 inner join 表2 on 表1.列名 = 表2.列名;
左连接:
select * from 表1 left join 表2 on 表1.列名 = 表2.列名;
右连接:
select * from 表1 right join 表2 on 表1.列名 = 表2.列名;
全连接:
select * from 表1 full outer join 表2 on 表1.列名 = 表2.列名;
子查询:
select * from 表名 where 列名 in (select 列名 from 其他表名);
存在查询:
select * from 表名 where exists (select 1 from 其他表名 where 条件);
联合查询:
select 列名 from 表1 union select 列名 from 表2;
获取当前时间:
select now();
获取当前日期:
select curdate();
日期加法:
select date_add(日期, interval 1 day);
日期减法:
select date_sub(日期, interval 1 day);
格式化日期:
select date_format(日期, '%y-%m-%d');
字符串连接:
select concat(列名1, 列名2) from 表名;
字符串长度:
select length(列名) from 表名;
字符串截取:
select substring(列名, 1, 5) from 表名;
查找字符串位置:
select locate('子串', 列名) from 表名;
大写转换:
select upper(列名) from 表名;
小写转换:
select lower(列名) from 表名;
去除空格:
select trim(列名) from 表名;
使用case语句:
select 列名, case when 条件 then '值1' else '值2' end from 表名;
使用if语句:
select 列名, if(条件, '值1', '值2') from 表名;
使用coalesce函数:
select coalesce(列名, '默认值') from 表名;
使用nullif函数:
select nullif(列名1, 列名2) from 表名;
获取唯一值的数量:
select count(distinct 列名) from 表名;
使用group_concat:
select group_concat(列名) from 表名 group by 其他列名;
事务开始:
begin;
提交事务:
commit;
回滚事务:
rollback;
创建游标:
declare 游标名 cursor for select 列名 from 表名;
打开游标:
open 游标名;
获取游标数据:
fetch 游标名 into 变量名;
关闭游标:
close 游标名;
创建存储过程:
create procedure 存储过程名 as begin ... end;
调用存储过程:
call 存储过程名();
创建函数:
create function 函数名() returns 数据类型 as begin ... end;
调用函数:
select 函数名();
创建触发器:
create trigger 触发器名 before insert on 表名 for each row set 新列 = '值';
删除触发器:
drop trigger 触发器名;
查询当前用户:
select current_user();
查询当前数据库:
select database();
查询表的行数和大小:
select table_name, table_rows, data_length from information_schema.tables where table_schema = '数据库名';
获取表的创建时间:
select create_time from information_schema.tables where table_name = '表名';
获取表的修改时间:
select update_time from information_schema.tables where table_name = '表名';
使用limit与order by结合:
select * from 表名 order by 列名 limit 10;
查询表的外键约束:
select constraint_name, table_name from information_schema.key_column_usage where table_schema = '数据库名';
查询表的主键约束:
select constraint_name, table_name from information_schema.table_constraints where table_schema = '数据库名' and constraint_type = 'primary key';
使用rollup进行分组汇总:
select 列名, sum(列名2) from 表名 group by 列名 with rollup;
获取前n条记录:
select * from 表名 limit n;
获取最后n条记录:
select * from 表名 order by 列名 desc limit n;
使用not exists进行条件判断:
select * from 表名 where not exists (select 1 from 其他表名 where 条件);
使用in进行条件判断:
select * from 表名 where 列名 in (值1, 值2);
使用not in进行条件判断:
select * from 表名 where 列名 not in (值1, 值2);
使用union all:
select 列名 from 表1 union all select 列名 from 表2;
使用explain分析查询:
explain select * from 表名 where 条件;
优化索引:
create index 索引名 on 表名 (列名);
使用临时表:
create temporary table 临时表名 as select * from 表名;
查询表的索引:
show index from 表名;
查询数据库版本:
select version();
捕获错误:
declare continue handler for sqlexception begin ... end;
输出错误信息:
select error_message();
使用事务处理错误:
begin; -- 开始事务 -- 执行sql语句 -- 如果有错误,rollback
备份数据库:
mysqldump -u 用户名 -p 数据库名 > 备份文件.sql
恢复数据库:
mysql -u 用户名 -p 数据库名 < 备份文件.sql
导入数据:
load data infile '文件路径' into table 表名;
导出数据:
select * into outfile '文件路径' from 表名;
显示当前数据库:
select database();
显示所有数据库:
show databases;
显示所有表:
show tables;
显示表结构:
describe 表名;
显示当前连接信息:
show processlist;
显示数据库使用情况:
select table_schema as '数据库', sum(data_length + index_length) / 1024 / 1024 as '大小(mb)' from information_schema.tables group by table_schema;
显示表的行数:
select count(*) from 表名;
显示用户权限:
show grants for '用户名'@'主机名';
到此这篇关于100条常用sql语句总结大全的文章就介绍到这了,更多相关常用sql语句内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论