元素

Good study, day day up!

« 关于css的命名习惯郁闷的情况 »

SQL SELECT 查询语句解析

一条SQL Select语句有几种元素构成
基础的有Form  Where  Order by

select * form…where…order by…

例如我们要查询数据库中"student"表中的"name"项数据

select name form student

同时我们可以针对"student"表中的若干项,用 ","号间隔,有返回的先后顺序

select id, name,sex,address,phone,birthday,age form student

 


当我们需要对所需数据做一些筛选时,我们通常应用where来实现这点
例如

select name from student where sex='man'

这样我们就实现了一个对性别为男的学生名称的查询,同样你可以反向查询,只需略微改动一下

select name  from student where sex<>'man'

查询生日在1980-01-01以后学生的各项名单

select name,sex,address,phone  from student  where birthday >='1981-01-01'

(不同语言对时间比对有不同的方式,以后在进行深入型研究,这里只做意思上的表达)
在运用条件时,还可以应用很多运算符如, "and " "or " " in"  "not"  "between and" "like"

select name,sex,address,phone  from student  where age='18' and age='20'
select name,sex,address,phone  from student  where age='18' or age='20'
select name,sex,address,phone  from student  where age between '18' and '20'
select name,sex,address,phone  from student  where age not between '18' and '20'
select name,sex,address,phone  from student  where adress in ('liaoning' , 'beijing','shanghai')
select name,sex,address,phone  from student  where name like 'Lee'

这里针对"like"做一些详细说明,因为它可以配合一些通配符进行模糊查询,如

"_"下划线
select name,sex,address,phone  from student  where name like '_ee'
可以查询名字是三个字母的,结尾两个字母为ee的学生信息,如"Tee" "Lee"
"%"百分号
select name,sex,address,phone  from student  where name like '%e"
可以查询名字以e结尾的所有学生信息,如果条件为'%e%',则是查询名字中任意位置包含e的所有学生信息
"[]"方括号
select name,sex,address,phone  from student  where name like '[TL]ee'
是一个与或条件,查询名字为三个字母的,结尾为ee,第一个字母是T或L的学生信息
"[^]"特殊符号
select name,sex,address,phone  from student  where name like '%[^d]e'
这是非条件,是查询最后一个是e,倒数第二个字母不是d的学生信息

 


当我们被要求以某个字段的升降序排列时,我们就用到了order by ,需要我们了解的就是ASC(升序)和DESC(降序),默认为升序

select name,sex,address,phone  from student  where sex='man' order by name ASC

也可以同时要求两个字段进行排序

select name,sex,address,phone  from student  where sex='man' order by name ASC, birthday DESC

这就是所查询的数据以名称升序查询

 


最后我还要加上一个符合条件

select top 10 name  from student

查询最新的十个学生信息

  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Walle Build 100427 Code detection by Codefense

Copyright2007-2010 Wenso.Com. All Rights Reserved.