select * from 表名 where 范围 --选择
where 字段 like '%value%' --包含value的内容,查找
select * from 表名 order by 字段 desc --排序
select sum(字段) as 新名字 from 表名 --求和
select avg(字段) as 新名字 from 表名 --求平均值
select max(字段) as 新名字 from 表名 --求最大值
select min(字段) as 新名字 from 表名 --求最小值
--增
insert into 表名 列名 values 列值 --插入单行
insert into 已有的新表名 列名 select 原来列名 from 原表名 --将现有表数据添加到一个已有表
--删
delete from 表名 where 删除条件 --删除满足条件的行
truncate table 表名 --将整个表删除 (删除表的所有行,但表的结构,列,约束,索引等不会被删除,不能用于有外键约束引用的表)
--改
update 表名 set 列名=更新值 where 更新的条件
--查
select 列名 from 表名 where 查询条件表达式 order by 排序的列名 asc或者desc --精确条件查询
select *from 表名 --查询所有的数据行和列
select 列名1,列名二,列明三 from 表名 where 查询条件表达式 --查询部分行列(条件查询)
select 列名 as 姓名 from 表名 where 查询条件表达式 --在查询中使用 AS 更改列名
select 列名 from 表名 where 查询条件表达式 order by desc或者asc --查询排序(先按条件查询 在进行升降排序)
--模糊查询
select * from 表名 where 列名 like ‘%字%’ --使用 like 进行模糊查询
select * from 表名 where 列名 between 数值 and 数值 --使用between在某个范围内进行查询
select 列名 from 表名 where 列名(address) in (‘上海’,‘山西’,‘山西’) --使用in在列举值内进行查询
--分组查询
select 列名1 as 汉字(学员编号),AVG(列名成绩) as 汉字(平均成绩) from 表名 group by 列名1 --使用 group by 进行分组查询
select 列1 as 汉字,AVG(列名2)as 汉字 from 表名 group by 列1 having count(列二)>1 --使用 having 子句进行分组筛选 (where只能在没有分组的里面使用,分组后只能使用having来限制条件)
--内连接
select 表1.name,表2.chengji from 表1,表2 where 表1.name=表2.name --在where子句中指定连接条件
select 表1.name,表2.chengji from 表1 inner join 表2 on 表1.name=表2.name --在from子句中使用join...on
sql server基本操作语句1
最新推荐文章于 2024-06-13 09:00:30 发布