–三秒之后显示’傻瓜你好’
waitfor delay ‘00:00:03’
print ‘傻瓜你好’
–GOTO语句
declare @n int
set @n = 1
loving:
print @n
set @n = @n + 1
while @n <= 3
goto loving
–切库
use DB1
–select后面写列名,from后面写表名
select *from stu3 --查询所有列
select name from stu3 --查询单个列
select id from stu3
select id ,name from stu3 --查询多个列
–select distinct 列名称 from 表名称
–针对english这一列去重复
select english from stu3
select distinct english from stu3
–top语句规定了返回的行数
–top n 用于指定查询结果返回的行数
select top 3 *from stu3 --可以有重复的3行数据
select distinct top 3 english from stu3 --返回没有重复的3行数据
– *代表所有的意思
select distinct top 3 *from stu3
–使用别名查询
–select 列名 ‘别名’ from 表名
–把列名给了一个别名进行显示,别名查询不会影响原有表的结果
select id ‘sno’ , age ‘nianlin’,name ‘xingming’
from stu3
–使用’='表达式
–select ‘别名’ = 列名 from 表名
select ‘nianling’ = age
from stu3
–使用as关键字来连接列表达式和指定的别名
–#############################用的最多################################
–select 列名 as ‘别名’ from 表名
select age as ‘nianling’
from stu3
–在进行数据查询时,经常需要对查询到的数据进行再次计算
–对某一列的数据进行更改,再次计算
select id,name,sex as ‘xingbie’,math -10 as ‘math’
from stu3
–选择查询,只是从整个表中选出满足指定条件的内容,这就用到where语句
–select select_list
–from table_list
–where search_conditions
select id
from stu3
where id = 1
select *from stu3
select * --查询所有行的话就用*
from stu3
where address = ‘香港’
–查询数学及格的人的信息
/*
1.确定表
2.确定要查询哪些列 所有列就是* 部分列就是选中的列
3.限制条件
*/
select *
from stu3
where math >=60
–逻辑运算符
–and 两边条件同时满足
–or 两边条件满足一个即可
select *
from stu3
where address = ‘杭州’ and sex = ‘男’ --查询出了马云爸爸
–选择查询
–介于两个指定值之间的所有数据
–查找数学成绩在60到80之间的学生
select *
from stu3
where math between 60 and 90
–列表搜索条件,in关键字只要满足列表中的任意一个条件就能返回结果
–把籍贯在北京和香港的人给找出来
select *
from stu3
where address in(‘北京’,‘香港’)
–like关键字搜索条件中的字符匹配字符,匹配字符串
–查找姓马的,% 代表0个或多个字符
select *
from stu3
where name like ‘马%’
–执行一个下划线 _ ,下划线代表一个字符
select *
from stu3
where name like ‘马_’
select *
from stu3
where name like ‘马__’ --两个下划线,两个字符
–查找名字中带有化字,或者景字的学生情况
select *
from stu3
where name like ‘[化景]%’
–^ 非符号的匹配
select *
from stu3
where name like '[^化景]%’
select *
from stu3
where name like ‘[马柳]%’
–涉及空值的查询(NULL)
select *from stu3
insert into stu3(id,name,age,sex,address,math,english)values
(10,‘李四’,43,‘女’,‘安徽’,NULL,34)
select *
from stu3
where math is null
–#############################聚合函数################################
–对一组值进行计算,例如对一列进行计算,并返回单个值
–sum 求和
–avg 平均值
–max 最大值
–min 最小值
–count(*):统计表中元组的个数
–先把为空的值删了
delete from stu3 where math = Null
delete from stu3 where id = 10
delete from stu3 where id = 9
–求成绩的总和sum()
–别名查询是为聚合函数做铺垫的 as ‘总数学成绩’ 随手增加一个别名
select SUM(math) as ‘总数学成绩’
from stu3
–求成绩的平均值
select AVG(math) as ‘数学平均成绩’
from stu3
select MAX(math) as ‘数学成绩最大值’
from stu3
–数据分组
–对查询的结果进行分组
–group by
–每个性别的总人数和男女分别的数学总分
select *from stu3
select sex,SUM(math) as ‘数学总分’
from stu3
group by sex
–查询男生和女生分别的数学最高分和最低分
–先写group by,再写select后面的语句
select sex,MAX(math) as ‘数学最高分’,MIN(math) as ‘数学最低分’
from stu3
group by sex
–having语句通常和group by一起使用
–查询总成绩小于150
本文详细介绍了SQL的基本查询语法,包括等待延迟、打印、循环、数据选择、条件过滤、逻辑运算、范围查询、列表搜索、模式匹配以及空值处理。此外,还讲解了聚合函数如SUM、AVG、MAX和MIN的使用,以及GROUP BY和HAVING子句在数据分组和统计分析中的应用。内容涵盖了数据库操作的各个方面,是理解SQL查询和数据库管理的基础教程。
917

被折叠的 条评论
为什么被折叠?



