sqlserver基础语句

owofile Lv5

sqlserver基础语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
select \* from Demo
--获取系统时间
select GETDATE()
--读取表里的生日
select birthady from Demo
--计算年龄
select DATEDIFF(yy,'2003-06-01',GETDATE())
--读取表里的生日 -- as 别名
select DATEDIFF(yy,birthady,GETDATE()) as '计算结果' from Demo; --如果没有from 表格 那么就无法定位到 名为 birthady的列 所以就会失败
-- 查询 [DATAEIFF() 函数返回两个日期的间隔 里面是 birthady日期 和 GETDATE 当前日期获取] 来自 Demo 表格
--全部查询
select \* from student
--条件查询
select name,age from student
where age>30
order by age desc
--起别名
--注意,as 可写可不写
select name as '姓名' ,age as '年龄' from student
where age>30
order by age desc
--加top
select top 2 name as '姓名' ,age as '年龄' from student
where age>30
order by age desc
--加常量列
select top 2 name as '姓名' ,age as '年龄' , '你好' as '还好' from student
where age>30
order by age desc
--加百分比查询 top
select top 50 percent name as '姓名' ,age as '年龄' , '你好' as '还好' from student
where age>30
order by age desc
--加百分比查询 加减操作
select top 50 percent name as '姓名' ,age + 50 as '年龄' , '你好' as '还好' from student
where age>30
order by age desc
-- is NULL 和 为空不一样
-- 查询条件为 值为NULL 或者 值为'空'
select \* from student
where phone is null or phone = ''
-- 检索 所有 学生表 as 别名Stu
select \* from Student as Stu
inner join score as Sco -- inner join 关联 成绩表 别名 as Sco
on Stu.Sid = Sco.id -- 关联条件 学生表的 Sid 和 成绩表的 id 进行关联 查询
--挑选结果
select Stu.name,Sco.name,Sco.age,Sco.sex,Sco.id from Student as Stu
inner join score as Sco
on Stu.Sid = Sco.id
--还可以写条件
where Sco.age=20
--------------
select \* from score as Sco -- 查询
left join Student as Stu -- 左边查询 没有显示NULL
on Stu.Sid = Sco.id
--右链接 取反
--求和
select SUM(age) as '学生年龄和' from Student
--最大值 最小值
select SUM(age) as '学生年龄和' from Student
select avg(age) as '学生平均值' from Student
select max(age) as '男同学的最大年龄'
from score where sex = '男'
--个数
select COUNT(\*) as '男同学个数' -- 一般来说括号里是 \* 或者是 1
from score where sex = '男'
select max(age) as '男同学的最大年龄'
from score where sex = '男'
----------------
select count(\*) as '女同学的个数'
from score where sex = '女'
----------------
select distinct(age) as '男同学的年龄都有那些'
from score where sex = '男'
--聚合函数 分组
select sex,COUNT(\*) as '个数' from score -- group by 为分组判断 他会把不一样的分组判断 计算个数出来 其中 group by 只负责分组 而 count负责计算
group by sex
--where 是分组前的条件
--having 是分组后的条件
select sex,COUNT(\*) as '个数' from score
where age > 1 -- 为前
group by sex
having COUNT(\*) < 1 -- 为后
--运行结果 满足此条件为 0
select \* from Usersinfo
where LoginName in ('lilei@163.com')
--使用连接查询登录名为 lilei@163.com 的用户所记笔记的笔记标题 内容和用户姓名
select Note.Title,Note.Content,Usr.UserName from Usersinfo as Usr
inner join NoteInfo as Note
on Usr.UserId = Note.NoteId
where Usr.LoginName = 'lilei@163.com'
--使用聚合函数查询登录名为 zhangtao@163.com 的用户且笔记分类名称是 工作 的笔记记录数
--聚合函数 分组
select CategoryName,COUNT(\*) as '个数' from NoteCategory -- group by 为分组判断 他会把不一样的分组判断 计算个数出来 其中 group by 只负责分组 而 count负责计算
group by CategoryName
--使用模糊查询 查询登录名为 lilei.163.com 的用户 且笔记标题里包含 邮件 的笔记标题和内容
select \* from Usersinfo
where LoginName like '%lilei.163.com%'
--按照创建时间降序排序 查询登录名为 lilei@163.com 的前10条笔记的标题和内容
--查询所有用户的姓名 登录名 笔记标题和笔记内容
--查询记录笔记数大于 1 的用户姓名 登录名和性别
use school
select \* from student;
--添加语句
insert into student values(2,3,4,5,6,7);
--添加语句
insert into student (name,sex,age)
values
(4,5,6)
--运行结果失败,是因为不允许为NULL 不然则可以
--多行插入
insert into student values
(4,5,6,7,8), --如果报红要加一个逗号
(5,6,7,8,9)
insert into Patient (password,BirthDate,Gender,PatientName,PhoneNum,Email,IndentityNum,Address)
values(123456,'1985-06-07','女','夏颖',13800000001,'ying.xia@qq.com',110000198506071100,'厦门市'),
(234567,'1985-06-08','男','李政',1380000002,'lizheng@163.com',210000198506082100,'长春市')
select \* from Patient;
--更新语句
UPDATE Patient set Password='11111'
--删除语句 范围是 id = 1;
delete from Patient where Patient=1;
--直接删除全部
delete from Patient
--但是序号都在
--用清除会连序号全部彻底删除
--去重 加入 distinct后 sex展示相同的只会显示一次
select distinct sex from Student
  • Title: sqlserver基础语句
  • Author: owofile
  • Created at : 2020-09-03 11:22:40
  • Updated at : 2025-04-11 21:18:25
  • Link: https://owofile.github.io/blog/2020/09/03/sqlserver/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
sqlserver基础语句