简单的select语句
Select语句的统计功能
Select语句中的多表连接
子查询
Select语句可以从数据库中按照用户的要求检索数据,并将查询结果以表格的形式输出。
Select具有的功能:数据查询、统计、分组、排序
三个表
Student(sno、sname、Ssex、Sage、Sdept)
Course(cno、cname、cpno(先修课)、Ccredit)
Sc(sno、cno、grade)
基本的select语句
1、查询所有的列:select * from student
2、查询表中指定的列:select sno,sname from student
消除值相同的行:distinct
查询选修了课程的学生的学号 select distinct sno from s
使用top [percent]返回前几行数据:
Select top 7 * from student
3、改变查询结果的列标题
对使用别名的操作有三种方式
(1)“列明 别名”
(2)“列名 as ‘别名’”
(3)“‘别名’=列名”
4、单引号的用处
使用单引号加入字符
Select aname,‘学号为:’,sno from s
Where子句
1、使用where来限定查询条件(给出限制查询的范围)
Where子句中的条件表达式
类别 |
运算符 |
说明 |
比较 |
=、>、<、<=、>=、<>、!= |
比较两个表达式 |
逻辑 |
And、or、not |
组合两个表达式的运算结果 或取反 |
范围 |
Between、not between |
搜索值是否在范围内 |
列表运算符 |
In、not in |
查询值是否属于列表值之一 |
字符运算符 |
Like、not like |
字符串是否匹配 |
未知值 |
Is null、is not null |
查询值是否为null |
例1、从学生表s中查询学号为“101”学生的学号和姓名
Select sno,sname from s where sno=‘101’
例2、从课程表c中查询课程名为“数据结构”而且学分为6分的课程信息
Select * from c where cname=‘数据结构’and credit=6
例3、从成绩表