数据库视频第六章

很高兴能为你分享我所学习到的!

简单的Select语句

语法格式

SELECT [ALL|DISTINCT]select_list'
[INTO new_table]
FROM table_source
[WHERE search_conditions]
[GROUP BY group_by_expression]
[HAVING search_conditions]
[ORDER BY order_expression[ASC|DESC]]

设置查询条件

举例:

select *from student_Info where student_name='超人'
‘查询student_info表中,姓名是"超人"的信息
select*from student_Info  where student_name='超人' and student_sex='男'
‘查询student_info表中,姓名是"超人"和性别是"男”的信息
select*from student_info where address like '中国%'
‘查询student_info表中,家庭住址以"中国"开头的信息
select*from student_Info  where student_name='超人' or student_sex='男'
‘查询student_info表中,姓名是"超人"或性别是"男”的信息

排序

举例

select * from result_Info where student_ID=1 
order by result desc’降序排列

select * from result_Info where student_ID=1 
order by result asc‘正序排列

分组

举例

select exam_no ,avg(result)from result_Info group by cube(exam_no,result) 
select exam_no ,avg(result)from result_Info group by rollup(exam_no,result)

使用函数

select max(result)from  result_Info  where exam_no ='2000期中'and student_id='1'
’查询对应表中,result列中最大的值
select avg(result)from  result_Info  where exam_no ='2000期中'and student_id='1'
’查询对应表中,result列中平均的值
select sum(result)from  result_Info  where exam_no ='2000期中'and student_id='1'
’查询对应表中,result列的总和
select top 3 result from  result_Info  where exam_no ='2000期中'and student_id='1' 
order by result [desc|adc]
‘求对应前三名按降序(升序排列)

使用Having子句

select exam_no,course_name,avg(result) from result_Info group by exam_no,course_name
having avg(result)>=70
order by exam_No 

注释:可以按照自己需求进行查询的结果

插入数据

语法格式:

INSERT[INTO]table_or_view[(column_list)] VALUES data_values

举例

insert into student_Info values ('122','姓名','性','2001-1-1','1','1','2009-1-1','1','')
’每一列都插入数据
insert into student_Info(student_ID ,student_Name ,address ) values ('122','姓名','1')
在选定的列中插入数据

INSERT…SELECT语句

语法格式“

INSERT table_name[column_list]
SELECT column_list
FROM table_list
WHERE search_condirions

举例:`

insert student1_Info
select *from student_Info  

SELECT…INTO语句

SELECT <select_list>
INTO new table 
FROM {<table_source>},[,...n]
WHERE <search_condition>

举例:

select *
into #student2
from student_Info  
where student_sex='男'

select*from #student2 

注意

SELECT…INTO语句和INSERT…SELECT语句的区别是:前者是存储在一个新表中,而后者是插入到一个原有的表中!

UPDATE语句

语法格式:

UPDATE [TOP] {table_name|view_name}
SET
{column name={expression|DEFAUIT|NULL}|@variable=expression}[,...n]
WHERE {search_conditions}

举例:

update student_Info set student_Name ='大梦'
where student_name ='奥拉朱旺'

在UPDATE语句使FROM子句

举例:

update student1_Info   set student_Sex =b.student_Sex ,address =b.address 
from student1_Info  a join student_Info  b on a.student_Name =b.student_Name 
where b.student_Sex ='女'

DELETE语句

DELETE FROM <table name>
[WHERE <search condition>]

举例:

delete from  student_Info 
where student_Name ='姓名'

TOP关键字和TOP表达式

语法格式:

[
TOP (expression)[PERCENT]
[WITH TIES]
]

举例:

declare @i int 
set @i =2
select top (2) with ties *from result_Info order by result 

COMPUTE子句

[
COMPUTE
{{AVG|COUNT|MAX|MIN|STDEV|STDEVP|VAR|VARP|SUM}
(expression)}[,...n]
[BY expression[,...n]
]

在WHERE子句使用运算符

在子句中可以使用:and or + - * % in 等等,不仅仅是只能使用等于

感谢您的光临,如果我有写的不对的地方,欢迎帮我找出来,谢谢!

猜你喜欢

转载自blog.csdn.net/qizhi666/article/details/81287526