当前位置: 代码迷 >> SQL >> 增删改操作
  详细解决方案

增删改操作

热度:69   发布时间:2016-05-05 09:59:41.0
常用SQL语句收藏 <一>

MSSQL 查询

select * from class;查询表class中的所有列.

select * from class; 查询表class中的所有列.select class,teacher from class;查询表class中的列class,teacherSelect count(*) as count_all from class;返回表class中的总行数给结果集.select sum(studentnumber) as all_student_number from class :返回class表中studentnumber的总数给结果集select avg(studentnumber) as avg_student_number  from class :返回class中studentnumber的平均值给结果集Select max(studentnumber)  as max_studentnumber from class :求某个字段的最大值,最小值.min是最小值.select * from class where studentnumber=(Select max(studentnumber) from class):求的最大值可以作为条件被引用.Select * from class where studentnumber=50(>50,>=50,<50,<=50,<>50):返回studentnumber=50 (>50,>=50,<50,<=50,不等于50):的记录Select * from class where studentnumber<>50 and teacher='li‘ 两个查询条件用and表示与,or表示或.Select * from class where studentnumber in (44,55) : in表示studentnumber为括号中所列的可能值.Select * from class where class in (select class from student) :in中的内容可以是另一个查询语句的结果.Select distinct class from student :查询字段的值不重复select * from class order by studentnumber (asc,desc) :对查询结果排序,可以按升序,也可以按降序.

续一

select class,count(*) from student group by class :查询结果根据group by 分组.select class,count(*) from student group by class having count(*)=5 :对分组的结果再用条件过滤select * from student where id<2UNION (ALL)select * from student where age>19 :UNION:将两个查询语句的查询结果放在一起,如果有重复的行,就删除,如果是UNION ALL:则重复的行不删除.模糊匹配查询: select * from student where name like '%ang%'整型,日期类型的字段可以指定范围.用betweenselect * from student where born between '1980-05-04' and '2983-10-18'select * ,12 from student 返回结果中增加一列,且值均为12.select RTRIM(class)+RTRIM(teacher) AS name1,studentnumber from class :将两个字段先删除尾部的空格,再连接起来返回. 其中:连接起来的字段名称返回时为name1Select class.*,student.* from class,student where class.class=student.class :两个表的内容联合起来查询,字段也可以用JOIN子句实现:select * from class JOIN student on class.class=student.classJOIN又分为内连接,外连接,左外连接,右外连接等,具体请查看相关的数据库的手册.


增删改操作

插入:指定字段名称及值的插入Insert into class (class,studentnumber,teacher) values('gaoer',55,'abc');不指定字段名称的插入Insert into class values('chuyi','abc',55);一次插入多条记录:只能执行多条insert语句.从另一个表中读出数据插入当前的表先创建一个新表:select * into class_bak from class where 1=2insert into class_bak select * from class where class='gaoer‘修改:Update class set class='gaoerer' where class='gaoer‘删除:Delete from class where class='gaoerer'



版权声明:本文为博主原创文章,未经博主允许不得转载。

  相关解决方案