sql查询代码Word下载.docx
- 文档编号:14873656
- 上传时间:2022-10-25
- 格式:DOCX
- 页数:12
- 大小:31.09KB
sql查询代码Word下载.docx
《sql查询代码Word下载.docx》由会员分享,可在线阅读,更多相关《sql查询代码Word下载.docx(12页珍藏版)》请在冰豆网上搜索。
,'
2010-09-01'
011'
山西'
null),('
011002'
景风'
1989-06-25'
重庆'
012001'
华丽佳'
女'
1988-05-20'
012'
大连'
null),
('
012002'
范治华'
1989-05-20'
山东'
013001'
李佳佳'
1990-03-01'
013'
湖南'
013002'
史慧敏'
1988-10-11'
湖北'
014001'
安静'
1991-03-23'
014'
014002'
尹强'
1987-06-02'
015001'
曹洪'
1989-05-16'
015'
015002'
杨世英'
1989-12-03'
天津'
null)
4在表course中插入数据
insertintocoursevalues('
1001'
电子商务基础'
72,2,1,null),('
2001'
英语'
72,3,0,null),
2002'
72,3,0,null),('
3001'
网页设计与制作'
72,2,1,null),
4001'
网络数据库'
72,3,1,null),('
5001'
电子商务安全与管理'
72,2,1,null)
5在表student_course中插入数据
insertintostudent_coursevalues(1,'
82,1),
(2,'
86,1),
(3,'
78,1),
(4,'
80,1),
(5,'
77,2),
(6,'
88,2),
(7,'
90,1),
(8,'
75,1),
(9,'
68,1),
(10,'
73,1),
(11,'
80,2),
(12,'
78,2),
(13,'
85,2),
(14,'
66,2),
(15,'
76,1),
(16,'
81,1),
(17,'
69,1),
(18,'
(19,'
70,2),
(20,'
89,2),
(21,'
(22,'
86,2),
(23,'
76,2),
(24,'
(25,'
(26,'
66,2)
7.2选择select查询结果集输出的列
7.2.2不能指定任何列
--用select语句显示常量、函数和表达式的值
select'
今天是’,getdate(),'
我的版本是’,@@version,1+2
7.2.3使用通配符(*)
--查询表student中的所有学生的全部信息
useStudentselect*fromstudents
7.2.4指定输出列
--查询表studes中的所有学生的学号和姓名selectstudent_id,student_namefromstudents
7.2.5使用列表达式
--在表student_course中查询学生的学号、课程号、成绩和新成绩(在原始成绩分数的基础上加分)
selectStudent_id,Course_id,Student_grade,Student_grade+5
fromstudent_course
7.2.6为查询结果指定列名
--为结果集中的列分别指定中文列名:
学号、课程号、原始成绩、新成绩
学号'
=Student_id,
Course_idas'
课程号'
Student_grade'
原始成绩'
Student_grade+5as'
新成绩'
7.3控制select查询结果集输出的行
--在表students中查询所有学生的性别
selectallStudent_sexfromstudents
2.使用Distinct参数
--在表students中查询所有学生的性别,要求消除重复数据selectdistinctStudent_sexfromstudents
7.3.2使用ORDERBY子句对结果集排序
Student_name)按照
--在表studens中查询所有学生的学生记录,并使显示的结果根据姓名字段(
不同的方式排序
--对结果集不使用任何其他排序查询
select*fromstudents
--使结果集按照姓名字段的默认的升序方式排序
select*fromstudentsorderbyStudent_name
--使结果集按照姓名字段的升序方式排序
select*fromstudentsorderbyStudent_nameasc
--使结果集按照姓名字段的降序方式排序
select*fromstudentsorderbyStudent_namedesc
--在表students中查询所有的学生记录,要求首先按照性别字段(别相同的按照姓名(Student_name)的升序排序
select*fromstudentsorderbyStudent_sexdesc*Student_nameasc
Student_sex)的降序排序,性
7.3.3使用TOP关键字输出部分记录
--在表Student中查询所有的学生记录,但是只显示前面的
3条记录
selecttop3*fromstudents
--在表Student中查询所有的学生记录,但是只显示结果集记录总数
33%的记录
selecttop33percent*fromstudents
--在表Student中查询所有的学生记录,结果集按性别的升序排序,但是只是显示前
selecttop3*fromstudentsorderbyStudent_sex
--在表Student中查询所有学生的记录,结果集按性别字段的升序排序,除了要显示外,还要显示岀与第3条记录的性别一样的所有其他记录
3前条记录
selecttop3withties*fromstudentsorderbyStudent_sex--没有ORDERBY不能使用WITHTIES
7.4使用WHER子句查询
>
大于
<
小于
=
等于
小于或等于
大于或等于
!
不等于
不小于
不大于
常用比较运算符
7.4.2使用单一查询条件
--在成绩表中查询所有学生的课程号为1001的课程的成绩
selectStudent_id,Course_id,Student_gradefromstudentcourse
whereCourseid='
7.4.3逻辑运算符
1.AND逻辑运算符
--查询课程号为1001的课程且成绩在分80以上的所有学生的成绩
selectStudent_id,Course_id,Student_gradefromstudent_coursewhereCourse_id='
andStudent_grade>
=80
2.OR逻辑运算符
--在成绩表中查询成绩在90分以上(含90)或成绩在70分以下(不含70)的所有学生的成绩selectStudent_id,Course_id,Student_grade
fromstudent_coursewhereStudent_grade>
=90orStudent_grade<
70
3.NOT逻辑运算符
--在成绩表中查询成绩在90分以上(含90)和成绩在70分以下(不含70),也就是不在70~90
分之间
selectStudent_id,Course_id,Student_gradefromstudent_coursewherenot(Student_grade>
=70andStudent_grade<
90)
4.BETWEEN和NOTBETWEEN逻辑运
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- sql 查询 代码