SQLserv单表查询

作者: gw

------------------------数据查询----------------------------

--select [all|distinct]<目标列的表达式>[,<目标列的表达式>]...
--from <表名或视图名>[,<表名或视图名>]...
--where <表达式>
--[group by <列名1>[having<条件表达式>]]
--[group by <列名2>[asc|desc]]

distinct  不显示重复的结果
asc      升序
desc    降序

--查询 所有学生的学号和姓名
--select sno,sname from student

--select sno,sname,sdept from student

--select * from student

--下面的 as brithday 是为计算后的列起一个名字
--select sname,2014-sage as brithday from student

--select sname ,‘year of birth:‘,2014-sage,upper(sdept) from student

--select sname ,‘year of birth:‘ as year_of_birth,2014-sage as year,upper(sdept) from student

--取消重复的行 distinct
--select sno from sc
--select distinct sno from sc

--select sname ,sdept from student where sdept=‘ma‘

--select sname ,sage from student where sage>21

--select sno from sc where grade<90

--select distinct sno from sc where grade<90

----查询条件
----比 较:=,>,<,!=,!
----确定范围:between and ,not between and
----确定集合:in , not in
----字符匹配:like, not like
----空 值: is null,is not null
----多重条件:and,or,not

-- between and 闭区间 包括头尾
--select sname ,sdept,sage from student where sage between 20 and 22

--select sname ,sdept,sage from student where sage not between 20 and 22

--in
--select sname ,sdept from student where sdept in(‘cs‘,‘ma‘)

--select sname ,sdept from student where sage in(20,21,22)

--select sname,ssex from student where sdept not in(‘cs‘,‘ma‘,‘is‘)

--like
--注意这里用%表示任意多字符而不是* 用_表示一个任意字符
--(!!如果是汉字的话 在ASCII中 __代表一个汉字, 在GBK中只需要一个_)
--select * from student where sname like ‘高%‘

--select * from student where sno like ‘2014%‘

--select * from student where sname like ‘王_‘

--字符串中最好不要有%或_ 自找苦吃

--涉及到空值得查询
--select sno,cno from sc where grade is null

--select sno,cno from sc where grade is not null

--多条件查询

--select sname,sdept,sage from student where sdept=‘ma‘ and sage>21

--select * from student where sdept=‘ma‘ and sage>21 and (ssex=‘男‘ or sdept=‘is‘)

--注意在多条件查询的过程中 执行顺序是 -> 从左到右的
--select * from student where sdept=‘ma‘ and sage>21 and ssex=‘男‘ or sdept=‘is‘

--order by 对查询结果进行排序升序asc 降序 desc
--select sno,grade from sc where cno=‘2‘ order by grade desc

--select sno,grade from sc where cno=‘2‘ order by grade

--select * from student order by sdept,sage desc

--函数
--select count(*)as sum from student

--select count(distinct sno) from sc
--select count( sno) from sc

--select avg(grade)as average_grade from sc

--select avg(grade)as average_grade from sc where cno=‘1‘

--select avg(grade) from sc where grade is null

--select max(grade) from sc

--select sum(grade) from sc

--group by
--把课程号相同的分成一组 然后统计每一组的人数
--select cno ,count(sno) from sc group by cno
--注意区别
--select count(distinct sno) from sc

--!!select cno, count(sno) from sc group by grade

--标注!! 的地方 或者有错或者是值得注意的地方

--分析下面的三句话是等价的
--select cno,count(cno) from sc group by cno
--select cno ,count(sno) from sc group by cno
--select cno ,count(*) from sc group by cno

--不明白 请看下面两句
--select cno ,sum(grade) from sc group by cno

--select cno ,avg(grade) from sc group by cno

--select cno ,sum(grade)as sum,avg(grade)as avg,count(distinct sno)as num from sc group by cno

--select sno,count(cno)as select_num from sc group by sno

--select sno from sc group by sno having count(*)>=3

--查询平均成绩大于80分的同学的学号和平均成绩
--select sno,avg(grade)as average_grade from sc group by sno having avg(grade)>=80

--上面是对单表的查询
--下面是对多表的查询

--查询每个学生及其选修课的情况

--select student.* ,sc.* from student,sc

-- = 等值连接
--select student.* ,sc.* from student ,sc where student.sno=sc.sno

-- 上面的 查询结果中 有两个sno出现 去掉的办法
--select student.* ,cno,grade from student ,sc where student.sno=sc.sno

-- 自身连接 例如查询现行课的先行课
select c1.cname ,c1.cno,c2.cpno from course c1,course c2 where c1.cpno=c2.cno

时间: 2024-08-09 06:21:24

SQLserv单表查询的相关文章

MySQL数据库实验二:单表查询

实验二   单表查询 一.实验目的 理解SELECT语句的操作和基本使用方法. 二.实验环境 是MS SQL SERVER 2005的中文客户端. 三.实验示例 1.查询全体学生的姓名.学号.所在系. SELECT Sname,S#,Sdept FROM S: 2.查询全体学生的详细记录. SELECT  * FROM S: 3.查全体学生的姓名及其出生年份. SELECT Sname,2011-Sage    /*假定当年的年份为2011年*/ FROM S: 4.查询选修了课程的学生学号.

【知了堂学习笔记】SQL查询基础语句(单表查询、多表查询)

SQL查询基础 1.单表查询 从数据库中查找数据 专业的称谓又称为投影 基本查询语句结构 select 列 from 表 * 所有列不是所有其他东西 查询所有数据 例:SELECT * FROM t_studen 需要执行比较细的操作  加上条件筛选:查询id为2号的学生信息 SELECT * FROM t_student WHERE id=2; 筛选的执行步骤 例:SELECT * FROM t_student WHERE id=2; SELECT *          (3) 再查询  筛选

python开发mysql:单表查询&amp;多表查询

一 单表查询,以下是表内容 1 一 having 过滤 2 1.1 having和where 3 select * from emp where id > 15; 4 解析过程;from > where 找到数据 > 分组(没有默认一个组)> select 打印 where是出结果之前 5 select * from emp having id > 15; 6 解析过程;from > where 找到数据(没有约束条件,就是整个表)) > 分组(没有默认一个组)&

mysql_05_单表查询

#创建数据库create database db_student; #使用数据库use db_student; #创建表create table `t_student` ( `id` int primary key not null auto_increment, `stuName` varchar (60), `age` int , `sex` varchar (30), `gradeName` varchar (60)); #插入数据insert into `t_student` (`id`

跟王老师学MySQL:单表查询

跟王老师学MySQL:单表查询 主讲教师:王少华   QQ群号:483773664 学习内容 查询所有字段 查询指定字段 查询指定记录 带in关键字的查询 带between and关键字的查询 带like关键字的查询 查询空值 带and的多条件查询 带or的多条件查询 查询不重复记录 对查询结果进行排序 单表查询是指从一张表中查询所需要的数据.查询数据时,可以从一张表中查询数据,也可以从多张表中同时查询数据.两者的查询方式上有一定的区别.因为单表查询只在一张表上进行操作,所以查询比较简单. 一.

mysql简单的单表查询详解

mysql简单的单表查询详解 MySQL的查询操作: 单表查询:简单查询 多表查询:连续查询 联合查询: 选择和投影: 投影:挑选要显示的字段 选择:挑选符合条件的行 投影:SELECT 字段1, 字段2, ... FROM tb_name;  SELECT * FROM tb_name; 选择:SELECT 字段1, ... FROM tb_name WHERE 子句; 布尔条件表达式 mysql> CREATE TABLE students (SID INT UNSIGNED AUTO_IN

mysql单表查询&amp;&amp;多表查询(职员表14+9)

dept(deptno,dname,loc) emp(empno,ename,job,mgr,hiredate,sal,COMM,deptno) salgrade(grade,losal,hisal) stu(sid,sname,age,gander,province,tuition) 单表查询题目 ==================================================== dept(deptno,dname,loc) emp(empno,ename,job,mgr

数据库原理-数据单表查询

格式:select [all|distinct] <目标列表达式>,[目标列]... from <表名或视图名>[,<表名或视图名>|(seslect 语句)[as]<别名>] [where <条件名>] [group by<列名1>[having <条件表达式>]] [order by<列名1>[asc|desc]] 单表查询 选择表中的若干列 1.查询全部列 select * from student 2.

第二章 单表查询 T-SQL语言基础(3)

单表查询(3) 2.6 处理字符数据 字符数据的查询处理,内容包括:类型,排序规则,运算符和函数,以及模式匹配. 2.6.1 数据类型 SQL Server支持两种字符数据类型----普通字符和Unicode字符.普通字符数据类型包括CHAR和VARCHAR,Unicode字符数据类型包括NCHAR和NVARCHAR. 普通字符和Unicode字符的区别:普通字符使用一个字节来保存每个字符,而Unicode字符则需要两个字节. 注:当表示一个普通的字符常量时,只需要简单地使用单引号:'This