建表语句及其插入相关数据

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
-- 建表
-- 学生表
CREATE TABLE Student(
s_id VARCHAR(20),
s_name VARCHAR(20) NOT NULL DEFAULT '',
s_birth VARCHAR(20) NOT NULL DEFAULT '',
s_sex VARCHAR(10) NOT NULL DEFAULT '',
PRIMARY KEY(s_id)
);
-- 课程表
CREATE TABLE Course(
c_id VARCHAR(20),
c_name VARCHAR(20) NOT NULL DEFAULT '',
t_id VARCHAR(20) NOT NULL,
PRIMARY KEY(c_id)
);
-- 教师表
CREATE TABLE Teacher(
t_id VARCHAR(20),
t_name VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(t_id)
);
-- 成绩表
CREATE TABLE Score(
s_id VARCHAR(20),
c_id VARCHAR(20),
s_score INT,
PRIMARY KEY(s_id,c_id)
);
-- 插入学生表测试数据
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
-- 课程表测试数据
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');

-- 教师表测试数据
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

-- 成绩表测试数据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);

Select练习题

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数 
# 内联查询学生, 然后查询学生成绩 ,最后比较两个学生的成绩
SELECT s.s_id,s.s_name,s1.s_score AS '01_score', s2.s_score AS '02_score'
FROM student s
INNER JOIN score s1 ON s.s_id = s1.s_id && s1.c_id = '01'
INNER JOIN score s2 ON s.s_id = s2.s_id && s2.c_id = '02'
WHERE s1.s_score > s2.s_score;

-- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
SELECT s.* ,s1.s_score '01score',s2.s_score '02score'
FROM student s
INNER JOIN score s1 ON s1.s_id = s.s_id && s1.c_id ='01'
INNER JOIN score s2 ON s2.s_id = s.s_id && s2.c_id = '02'
WHERE s1.s_score < s2.s_score;

-- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT s.*, AVG(sc.s_score) 'avg'
FROM student s
INNER JOIN score sc ON sc.s_id = s.s_id
GROUP BY s.s_id, s.s_name
HAVING AVG(sc.s_score) >= 60;


-- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩 (包括有成绩的和无成绩的)
SELECT s.*, AVG(IFNULL(sc.s_score,0)) 'avg'
FROM student s
INNER JOIN score sc ON sc.s_id = s.s_id
GROUP BY s.s_id, s.s_name
HAVING AVG(IFNULL(sc.s_score,0)) <= 60 ;



-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
SELECT s.* ,SUM(sc.s_score) 'sumS' ,COUNT(sc.c_id) 'count'
FROM student s
INNER JOIN score sc ON sc.s_id = s.s_id
GROUP BY s.s_id
HAVING COUNT(sc.c_id) && SUM(sc.s_score);

-- 6、查询"李"姓老师的数量

SELECT COUNT(teacher.t_id) '数量'
FROM teacher
WHERE teacher.t_name LIKE '李%';

-- 7、查询学过"张三"老师授课的同学的信息
# 先查询张三老师的t_id,然后再找出score中t_id = 李老师id 的学习id, 然后根据id查询出学生信息


SELECT s.*
FROM student s
INNER JOIN score sc ON sc.s_id = s.s_id
INNER JOIN course c ON c.c_id = sc.c_id
INNER JOIN teacher t ON t.t_id = c.t_id
WHERE t.t_name = '张三';



-- 8、查询没学过"张三"老师授课的同学的信息

SELECT * FROM student WHERE s_id not IN (
SELECT s.s_id
FROM student s
INNER JOIN score sc ON sc.s_id = s.s_id
INNER JOIN course c ON sc.c_id = c.c_id
INNER JOIN teacher t ON c.c_id = t.t_id
WHERE t.t_name = '张三'
);

-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
SELECT s.*
FROM student s
INNER JOIN score sc1 ON sc1.s_id = s.s_id
INNER JOIN score sc2 ON sc2.s_id = s.s_id
WHERE sc1.c_id = '01' && sc2.c_id = '02';


-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息


SELECT s.*
FROM student s
WHERE
s.s_id IN (SELECT s_id FROM score WHERE c_id = '01')
&&
s.s_id not IN (SELECT s_id FROM score WHERE c_id = '02');


-- 11、查询没有学全所有课程的同学的信息
# 总共有三个课程,只需要查询课程数不等于三的学生id即可得出
# SELECT COUNT(c_id) count1 FROM course;
# SELECT COUNT(c_id) count2 FROM score WHERE s_id = 08;
SELECT s.*
FROM student s
WHERE
(SELECT COUNT(c_id) count1 FROM course)
!=
(SELECT COUNT(c_id) count2 FROM score
WHERE s_id = s.s_id);



-- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
# 直接查询一门都没有的 ,然后取反
-- SELECT s.c_id FROM score s
-- WHERE s.s_id = '01'
--
-- SELECT DISTINCT s.*
-- FROM student s
-- INNER JOIN score sc ON sc.s_id = s.s_id
-- WHERE sc.c_id IN (SELECT c_id FROM score WHERE s_id = '01');
# ---------------------

SELECT * FROM student WHERE s_id NOT IN
(
SELECT DISTINCT s.s_id
FROM student s
INNER JOIN score sc ON sc.s_id = s.s_id
WHERE sc.c_id IN (SELECT c_id FROM score WHERE s_id = '01')
);

-- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
# 查询出01学生的选的课程为3个,所以直接找选的课程数为3 的

SELECT DISTINCT s.*
FROM student s
INNER JOIN score sc ON sc.s_id = s.s_id
WHERE (SELECT COUNT(c_id) FROM score
WHERE s_id = sc.s_id) = (SELECT COUNT(c_id) FROM score
WHERE s_id = 01);

-- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
# 也就是根据张三的id ,查询选课中t_id有张三id的学生id ,然后在 not in 即可
SELECT s.s_name
FROM Student s
WHERE s.s_id NOT IN (
SELECT sc.s_id
FROM Score sc
JOIN Course c ON c.c_id = sc.c_id
JOIN Teacher t ON t.t_id = c.t_id
WHERE t.t_name = '张三'
);

-- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

# 先查询不及格的学生然后按照学号分组,再求出数量大于等于2的学生,最后求平均成绩
SELECT s.s_id, s.s_name, AVG(sc.s_score) AS avg_score
FROM Student s
INNER JOIN Score sc ON s.s_id = sc.s_id
WHERE sc.s_score < 60
GROUP BY s.s_id
HAVING COUNT(*) >= 2;



-- 16、检索"01"课程分数小于60,按分数降序排列的学生信息

SELECT s.* FROM student s
INNER JOIN score sc ON sc.s_id = s.s_id
WHERE sc.c_id = '01' && sc.s_score <= 60
ORDER BY sc.s_score DESC;




-- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECT sc.s_id, sc.s_score,AVG(sc.s_score) avg
FROM score sc
INNER JOIN course c ON sc.c_id = c.c_id
GROUP BY sc.c_id, sc.s_id
ORDER BY avg DESC;


-- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
-- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
SELECT c.*, MAX(sc.s_score) max, MIN(sc.s_score) min , AVG(sc.s_score) avg ,
CONCAT(SUM(CASE WHEN sc.s_score >=60 THEN 1 ELSE 0 END) / COUNT(sc.c_id) * 100,'%') 及格率,
CONCAT(SUM(CASE WHEN sc.s_score >=70 && sc.s_score < 80 THEN 1 ELSE 0 END) / COUNT(sc.c_id) * 100,'%') 中等率 ,
CONCAT(SUM(CASE WHEN sc.s_score >=80 && sc.s_score < 90 THEN 1 ELSE 0 END) / COUNT(sc.c_id) * 100,'%') 优良率 ,
CONCAT(SUM(CASE WHEN sc.s_score >= 90 THEN 1 ELSE 0 END) / COUNT(sc.c_id) * 100,'%') 优秀率
FROM course c
INNER JOIN score sc ON sc.c_id = c.c_id
GROUP BY sc.c_id;


-- 19、按各科成绩进行排序,并显示排名(实现不完全)
SELECT
s1.s_id,
s1.c_id,
s1.s_score
-- (COUNT(DISTINCT s2.s_score) + 1) rank
FROM score s1
INNER JOIN score s2 ON s1.c_id = s2.c_id && s1.s_score > s2.s_score
GROUP BY s1.s_id, s1.c_id, s1.s_score
ORDER BY s1.c_id, s1.s_score DESC;

-- 20、查询学生的总成绩并进行排名
SELECT s.* , SUM(sc.s_score) sum,
RANK() OVER(ORDER BY SUM(sc.s_score) DESC) 'rank'
FROM student s INNER JOIN score sc ON sc.s_id = s.s_id
GROUP BY s.s_id
ORDER BY sum DESC;

-- 21、查询不同老师所教不同课程平均分从高到低显示

SELECT c.c_id ,c.t_id ,AVG(sc.s_score) avg
FROM course c
INNER JOIN score sc ON sc.c_id = c.c_id
GROUP BY c.t_id , c.c_id
ORDER BY avg DESC;

-- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
SELECT s.* ,sc.c_id, c.c_name,sc.s_score
FROM student s
INNER JOIN score sc ON sc.s_id = s.s_id
INNER JOIN course c ON c.c_id = sc.c_id
WHERE (
SELECT COUNT(DISTINCT s_score)
FROM Score
WHERE c_id = c.c_id AND s_score > sc.s_score
) BETWEEN 1 AND 2
ORDER BY c.c_id, sc.s_score DESC;

-- 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比

SELECT c.c_id, c.c_name,
COUNT(CASE WHEN s.s_score >= 85 AND s.s_score <= 100 THEN 1 END) AS '100-85',
COUNT(CASE WHEN s.s_score >= 70 AND s.s_score < 85 THEN 1 END) AS '85-70',
COUNT(CASE WHEN s.s_score >= 60 AND s.s_score < 70 THEN 1 END) AS '70-60',
COUNT(CASE WHEN s.s_score >= 0 AND s.s_score < 60 THEN 1 END) AS '0-60',
COUNT(*) AS total_count,
CONCAT(COUNT(CASE WHEN s.s_score >= 85 AND s.s_score <= 100 THEN 1 END) * 100.0 / COUNT(*), '%') AS '100-85%',
CONCAT(COUNT(CASE WHEN s.s_score >= 70 AND s.s_score < 85 THEN 1 END) * 100.0 / COUNT(*), '%') AS '85-70%',
CONCAT(COUNT(CASE WHEN s.s_score >= 60 AND s.s_score < 70 THEN 1 END) * 100.0 / COUNT(*), '%') AS '70-60%',
CONCAT(COUNT(CASE WHEN s.s_score >= 0 AND s.s_score < 60 THEN 1 END) * 100.0 / COUNT(*), '%') AS '0-60%'
FROM Score s
INNER JOIN Course c
ON s.c_id = c.c_id
GROUP BY c.c_id, c.c_name;

-- 24、查询学生平均成绩及其名次

SELECT s_id, AVG(s_score) AS avg_score,
RANK() OVER (ORDER BY AVG(s_score) DESC) AS rank
FROM Score
GROUP BY s_id
ORDER BY rank DESC;

-- 25、查询各科成绩前三名的记录
-- 1.选出b表比a表成绩大的所有组
-- 2.选出比当前id成绩大的 小于三个的
select a.c_id,a.s_id,a.s_score from score a
left join score b on a.c_id = b.c_id and a.s_score<b.s_score
group by a.s_id,a.c_id,a.s_score HAVING COUNT(b.s_id)<3
ORDER BY a.c_id,a.s_score DESC



-- 26、 查询每门课程被选修的学生数
SELECT sc.c_id, c.c_name,COUNT(sc.s_id) count
FROM score sc
INNER JOIN course c ON sc.c_id = c.c_id
GROUP BY sc.c_id;


-- 27、查询出只有两门课程的全部学生的学号和姓名
SELECT s.*
FROM student s INNER JOIN score sc ON sc.s_id = s.s_id
GROUP BY s.s_id
HAVING COUNT(sc.c_id) = 2;

-- 28、查询男生、女生人数
SELECT DISTINCT COUNT(s.s_id) '男', COUNT(s.s_id) '女'
FROM student s GROUP BY s.s_sex;



-- 29、查询名字中含有"风"字的学生信息
SELECT s.* FROM student s
WHERE s.s_name LIKE '%风%';

-- 30、查询同名同性学生名单,并统计同名人数
SELECT s_name, s_sex, COUNT(*) 'num'
FROM student
GROUP BY s_name, s_sex
HAVING COUNT(*) > 1;

-- 31、查询1990年出生的学生名单
SELECT s.* FROM student s
WHERE s.s_birth LIKE '%1990%';


-- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECT sc.c_id , AVG(sc.s_score) avg
FROM score sc
GROUP BY sc.c_id
ORDER BY avg DESC, sc.c_id ASC;

-- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
SELECT s.s_id ,s.s_name , AVG(sc.s_score) avg
FROM student s INNER JOIN score sc ON sc.s_id = s.s_id
GROUP BY s.s_id ,s.s_name
HAVING AVG(sc.s_score) > 85;

-- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数
SELECT c_id
FROM course WHERE c_name = '数学'

SELECT s.s_id ,s.s_name, sc.c_id,sc.s_score
FROM student s INNER JOIN score sc ON sc.s_id = s.s_id
GROUP BY s.s_id, s.s_name, sc.c_id
HAVING sc.c_id = (SELECT c_id
FROM course WHERE c_name = '数学') && sc.s_score < 60;


-- 35、查询所有学生的课程及分数情况
SELECT s.s_id , s.s_name ,sc.c_id ,c.c_name,sc.s_score
FROM student s
INNER JOIN score sc ON sc.s_id = s.s_id
INNER JOIN course c ON c.c_id = sc.c_id;


-- 36、查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数;
SELECT s.s_id ,s.s_name ,c.c_name ,sc.s_score
FROM student s
INNER JOIN score sc ON sc.s_id = s.s_id
INNER JOIN course c ON c.c_id = sc.c_id
WHERE sc.s_score >= 70;


-- 37、查询不及格的课程
SELECT s.s_id ,s.s_name ,c.c_name ,sc.s_score
FROM student s
INNER JOIN score sc ON sc.s_id = s.s_id
INNER JOIN course c ON c.c_id = sc.c_id
WHERE sc.s_score < 60;
-- 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
SELECT s.*, sc.s_score FROM student s
INNER JOIN score sc ON sc.s_id = s.s_id
WHERE sc.c_id = '01' && sc.s_score >= 80;

-- 39、求每门课程的学生人数
SELECT c.c_id ,c.c_name , COUNT(sc.s_id) '人数'
FROM course c INNER JOIN score sc ON sc.c_id = c.c_id
GROUP BY c.c_id,c.c_name;
-- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT s.s_id,s.s_name,sc.c_id, MAX(sc.s_score) max, sc.s_score
FROM student s INNER JOIN score sc ON sc.s_id = s.s_id
GROUP BY s.s_id, s.s_name, sc.c_id, sc.s_score
HAVING sc.c_id = (
SELECT c_id FROM course WHERE t_id = (
SELECT t_id FROM teacher WHERE t_name = '张三')
)LIMIT 1;
# 两种方式
SELECT Student.s_id, Student.s_name, Score.s_score
FROM Student
JOIN Score ON Student.s_id = Score.s_id
JOIN Course ON Score.c_id = Course.c_id
JOIN Teacher ON Course.t_id = Teacher.t_id
WHERE Teacher.t_name = '张三'
ORDER BY Score.s_score DESC
LIMIT 1;

-- 41、查询不同课程成绩相同的同一个学生的学生编号、课程编号、学生成绩
# 通过创建副本, 然后进行筛选
SELECT s1.s_id, sc1.c_id, sc1.s_score
FROM Score sc1, Score sc2, Student s1, Student s2
WHERE sc1.s_id = s1.s_id AND sc2.s_id = s2.s_id AND sc1.s_id <> sc2.s_id
AND sc1.c_id <> sc2.c_id AND sc1.s_score = sc2.s_score;


-- 42、查询每门功成绩最好的前两名
# 在where判断语句中进行判断
SELECT s1.c_id, s1.s_id, s1.s_score
FROM Score s1
WHERE (SELECT COUNT(*) FROM Score s2
WHERE s2.c_id = s1.c_id AND s2.s_score > s1.s_score) < 2;

-- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT sc.c_id,COUNT(sc.s_id) '人数'
FROM score sc
GROUP BY sc.c_id
ORDER BY COUNT(sc.s_id) DESC ,sc.c_id ASC;



-- 44、检索至少选修两门课程的学生学号
SELECT sc.s_id ,COUNT(sc.c_id) 'num'
FROM score sc
GROUP BY sc.s_id
HAVING COUNT(sc.c_id) >= 2;


-- 45、查询选修了全部课程的学生信息
SELECT s.*
FROM score sc INNER JOIN student s WHERE s.s_id = sc.s_id
GROUP BY sc.s_id
HAVING COUNT(sc.c_id) = (SELECT COUNT(c_id) FROM course);


-- 46、查询各学生的年龄

# CURDATE() 函数返回当前日期,YEAR() 函数用于从日期中提取年份。DATEDIFF() 函数用于计算两个日期之间的天数差,然后将其与当前日期进行比较,以确定出生日期是否在当前日期之后。
# 如果出生日期在当前日期之后,则相差的年数需要减 1。 (DATEDIFF(CURDATE(), s.s_birth) < 0)
SELECT s.* , YEAR(CURDATE()) - YEAR(s.s_birth) 'age'
FROM student s;
--
-- SELECT s.*, DATEDIFF(CURDATE(), s.s_birth)
-- FROM student s ;
SELECT s.*, YEAR(CURDATE()) - YEAR(s.s_birth)
FROM student s ;

-- 47、查询本周过生日的学生
SELECT s_name, s_birth
FROM Student
WHERE WEEK(s_birth) = WEEK(CURDATE()) AND DATE_FORMAT(s_birth, '%m-%d') >= DATE_FORMAT(CURDATE(), '%m-%d')
ORDER BY s_birth ASC;

-- 48、查询下周过生日的学生
SELECT s_name, s_birth
FROM Student
WHERE WEEK(s_birth) = WEEK(DATE_ADD(CURDATE(), INTERVAL 1 WEEK)) AND DATE_FORMAT(s_birth, '%m-%d') >= DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL 1 WEEK), '%m-%d')
ORDER BY s_birth ASC;
-- 49、查询本月过生日的学生

SELECT s_name, s_birth
FROM Student
WHERE MONTH(s_birth) = MONTH(CURDATE()) AND DATE_FORMAT(s_birth, '%d') >= DATE_FORMAT(CURDATE(), '%d')
ORDER BY s_birth ASC;
-- 50、查询下月过生日的学生
SELECT s_name, s_birth
FROM Student
WHERE MONTH(s_birth) = MONTH(DATE_ADD(CURDATE(), INTERVAL 1 MONTH)) AND DATE_FORMAT(s_birth, '%d') >= DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL 1 MONTH), '%d')
ORDER BY s_birth ASC;

MySQL下出现的几个问题

1
2
-- 19、按各科成绩进行排序,并显示排名(实现不完全)	
-- 47、查询本周过生日的学生

安全性练习题

在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
use test
--1.创建登陆帐户(登录账户名:你的名字缩写(‘例如zs’), 密码:你的名字缩写,默认数据库:test)
create login zs with password='zs', default_database=test

--2.为登陆账户创建数据库用户,在test数据库中的'安全性'中的‘用户’下可以找到新创建的zs
execute sp_grantdbaccess zs


--3.通过加入数据库角色,赋予zs用户“db_owner”这个角色的权限
exec sp_addrolemember 'db_owner', 'zs'


--4.创建一个查询角色SelectRole,并分配test数据库下4个表的查询权限。

exec sp_addrole SelectRole

grant select on Course to SelectRole
grant select on Score to SelectRole
grant select on Student to SelectRole
grant select on Teacher to SelectRole

--5.创建一个角色SelectandInsertRole,并分配test数据库下4个表的查询,插入权限。
exec sp_addrole SelectandInsertRole

grant insert,select on Course to SelectandInsertRole
grant insert,select on Score to SelectandInsertRole
grant insert,select on Student to SelectandInsertRole
grant insert,select on Teacher to SelectandInsertRole


--6.创建一个角色TestManagerRole,并分配test数据库下4个表的所有权限。
exec sp_addrole TestManagerRole

grant all privileges on Course to TestManagerRole
grant all privileges on Score to TestManagerRole
grant all privileges on Student to TestManagerRole
grant all privileges on Teacher to TestManagerRole

--7.为用户zs分配所有test中4个表的所有权限,并允许zs将所拥有的权限授予其他用户
grant all on Course to zs
with grant option
grant all on Score to zs
with grant option
grant all on Student to zs
with grant option
grant all on Teacher to zs
with grant option

--8.回收用户zs的test中4个表的查看权限
revoke select on Course from zs cascade
revoke select on Score from zs cascade
revoke select on Student from zs cascade
revoke select on Teacher from zs cascade