码迷,mamicode.com
首页 > 数据库 > 详细

常用SQL语句收藏 <一>

时间:2015-08-27 13:25:42      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:sql 常用操作收藏   ms sql   

MSSQL 查询

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

select * from class; 查询表class中的所有列.
select class,teacher from class;查询表class中的列class,teacher
Select 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<2
UNION (ALL)
select * from student where age>19 :UNION:将两个查询语句的查询结果放在一起,如果有重复的行,
就删除,如果是UNION ALL:则重复的行不删除.
模糊匹配查询: select * from student where name like '%ang%'
整型,日期类型的字段可以指定范围.用between
select * 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 :将两个字段先删除尾部的空格,再连接起来返回. 其中:连接起来的字段名称返回时为name1
Select class.*,student.* from class,student where class.class=student.class :两个表的内容联合起来查询,字段
也可以用JOIN子句实现:select * from class JOIN student on class.class=student.class
JOIN又分为内连接,外连接,左外连接,右外连接等,具体请查看相关的数据库的手册.


增删改操作

插入:
指定字段名称及值的插入
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=2
insert into class_bak select * from class where class='gaoer‘
修改:Update class set class='gaoerer' where class='gaoer‘
删除:Delete from class where class='gaoerer'



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

常用SQL语句收藏 <一>

标签:sql 常用操作收藏   ms sql   

原文地址:http://blog.csdn.net/yxh1157686920/article/details/48025617

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!