简单SQL语句小结

2016-01-29 16:03 6 1 收藏

简单SQL语句小结,简单SQL语句小结

【 tulaoshi.com - SQLServer 】

为了大家更容易理解我举出的SQL语句,本文假定已经建立了一个学生成绩管理数据库,全文均以学生成绩的管理为例来描述。

  1.在查询结果中显示列名:

  a.用as关键字:select name as '姓名' from students order by age

  b.直接表示:select name '姓名' from students order by age

  2.精确查找:

  a.用in限定范围:select * from students where native in ('湖南', '四川')

  b.between...and:select * from students where age between 20 and 30

  c.“=”:select * from students where name = '李山'

  d.like:select * from students where name like '李%' (注意查询条件中有“%”,则说明是部分匹配,而且还有先后信息在里面,即查找以“李”开头的匹配项。所以若查询有“李”的所有对象,应该命令:'%李%';若是第二个字为李,则应为'_李%'或'_李'或'_李_'。)

  e.[]匹配检查符:select * from courses where cno like '[AC]%' (表示或的关系,与"in(...)"类似,而且"[]"可以表示范围,如:select * from courses where cno like '[A-C]%')

  3.对于时间类型变量的处理

  a.smalldatetime:直接按照字符串处理的方式进行处理,例如:

  select * from students where birth = '1980-1-1' and birth <= '1980-12-31'

  4.集函数

  a.count()求和,如:select count(*) from students (求学生总人数)

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/sqlserver/)

  b.avg(列)求平均,如:select avg(mark) from grades where cno=’B2’

  c.max(列)和min(列),求最大与最小

  5.分组group

  常用于统计时,如分组查总数:

  select gender,count(sno)
from students
group by gender

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/sqlserver/)

  (查看男女学生各有多少)

  注意:从哪种角度分组就从哪列"group by"

  对于多重分组,只需将分组规则罗列。比如查询各届各专业的男女同学人数 ,那么分组规则有:届别(grade)、专业(mno)和性别(gender),所以有"group by grade, mno, gender"

  select grade, mno, gender, count(*)
from students
group by grade, mno, gender

  通常group还和having联用,比如查询1门课以上不及格的学生,则按学号(sno)分类有:

来源:https://www.tulaoshi.com/n/20160129/1497256.html

延伸阅读
说明:复制表(只复制结构,源表名:a 新表名:b) select * into b from a where 11 说明:拷贝表(拷贝数据,源表名:a 目标表名:b) insert into b(a, b, c) select d,e,f from b; 说明:显示文章、提交人和最后回复时间 select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title...
1.调整内存 sp_configure 'show advanced options',1 GO RECONFIGURE Go sp_configure 'awe enabled', 1 GO RECONFIGURE Go sp_configure 'min server memory',1024 Go sp_configure 'max server memory',3072 GO RECONFIGURE Go PS: OS需要打开AWE 即在boot.ini里 添加 /3G 或者 /PAE (企业版)才能支持4G以上内存! 2.关...
都是一些精典实用的常用语句. MYSQL显示数据库或表: 以下为引用的内容: show databases;//然后可以use database_name; show tables; MYSQL更改表名: alter table table_name rename new_t; MYSQL添加列 : alter table table_name add column c_n column attributes; MYSQL删除列: alter table tabl...
标签: MySQL mysql数据库
2006-10-8 表数据:tab1 id    name  num A     a         9 A     b       11 B     f         7 B     g  ...
1、 代码如下: select top 10 * from ( select top (@Page * 10) ROW_NUMBER() OVER (order by id) as RowNum, id, username from Guest where username = 'user' ) as T where RowNum ((@Page - 1) * 10) 2、 代码如下: select * from ( select ROW_NUMBER() OVER(order by id) as RowNum,id,username from Guest where user...

经验教程

852

收藏

92
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部