最全50个Mysql数据库查询练习题

此数据库查询语句是网络上50个数据库查询练习题目,网上有些版本是oracle语句写的,大多数公司还是用免费的mysql数据库,以下都是mysql版本,全部都有验证过。

表名和字段

–1.学生表
Student(s#, sname, sage,ssex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c#,cname,t#) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t#,tname) –教师编号,教师姓名
–4.成绩表
Sc(s#,c#,score) –学生编号,课程编号,分数

测试数据

用数据库可视化工具做练习非常方便,推荐使用sqlyog,软件图标是一只海豚。

在新连接种填上本机地址,用户名,密码和端口就直接连上mysql。

所有测试数据如下:

# --插入学生表测试数据
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 sc VALUES('01' , '01' , 80);
INSERT INTO sc VALUES('01' , '02' , 90);
INSERT INTO sc VALUES('01' , '03' , 99);
INSERT INTO sc VALUES('02' , '01' , 70);
INSERT INTO sc VALUES('02' , '02' , 60);
INSERT INTO sc VALUES('02' , '03' , 80);
INSERT INTO sc VALUES('03' , '01' , 80);
INSERT INTO sc VALUES('03' , '02' , 80);
INSERT INTO sc VALUES('03' , '03' , 80);
INSERT INTO sc VALUES('04' , '01' , 50);
INSERT INTO sc VALUES('04' , '02' , 30);
INSERT INTO sc VALUES('04' , '03' , 20);
INSERT INTO sc VALUES('05' , '01' , 76);
INSERT INTO sc VALUES('05' , '02' , 87);
INSERT INTO sc VALUES('06' , '01' , 31);
INSERT INTO sc VALUES('06' , '03' , 34);
INSERT INTO sc VALUES('07' , '02' , 89);
INSERT INTO sc VALUES('07' , '03' , 98);

最后是50个数据库查询练习,已经验证过,是mysql版本的。

1. 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数

SELECT * FROM
  (SELECT `s#` AS sno1, `c#`AS cno1, score FROM sc WHERE `c#`=01) a
    LEFT JOIN
  (SELECT `s#` AS sno2, `c#`AS cno2, score FROM sc WHERE `c#`=02) b
    ON a.sno1 = b.sno2
    WHERE a.score > b.score

1.1 查询同时存在" 01 "课程和" 02 "课程的情况

SELECT * FROM
  (SELECT `s#` AS sno1, `c#`AS cno1, score FROM sc WHERE `c#`=01) a
    LEFT JOIN
  (SELECT `s#` AS sno2, `c#`AS cno2, score FROM sc WHERE `c#`=02) b
    ON a.sno1 = b.sno2
    WHERE sno2 IS NOT NULL

1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )

SELECT * FROM
  (SELECT `s#` AS sno1, `c#`AS cno1, score FROM sc WHERE `c#`=01) a
    LEFT JOIN
  (SELECT `s#` AS sno2, `c#`AS cno2, score FROM sc WHERE `c#`=02) b
    ON a.sno1 = b.sno2

1.3 查询不存在" 01 "课程但存在" 02 "课程的情况

SELECT * FROM
    sc WHERE `c#`='02' AND `s#` NOT IN (SELECT `s#` FROM sc WHERE `c#`='01')

2. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

SELECT a.`s#`,b.`sname`, a.avg_score FROM
  (SELECT `s#` ,AVG(score) AS avg_score FROM sc GROUP BY `s#`) AS a
    LEFT JOIN student AS b
    ON a.`s#` = b.`s#`
    WHERE a.avg_score >=60

3. 查询在 SC 表存在成绩的学生信息

SELECT * FROM student WHERE `s#` IN (SELECT DISTINCT `s#` FROM sc)

4. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 NULL )

SELECT `s#` ,sname , course_num , score_sum FROM
  (SELECT `s#`, sname FROM student ) AS a
    LEFT JOIN
  (SELECT `s#` AS sno ,COUNT(`c#`) AS course_num ,SUM(score) AS score_sum FROM sc GROUP BY sno) AS b
    ON a.`s#` = b.sno

4.1 查有成绩的学生信息

# 在最外面一层select的时候,不可以用函数
# 如果两张表连接之后,有相同的字段,这时候select就需要把其中一个字段改名

SELECT `s#` ,sname , course_num , score_sum FROM
  (SELECT `s#`, sname FROM student ) AS a
    LEFT JOIN
  (SELECT `s#` AS sno ,COUNT(`c#`) AS course_num ,SUM(score) AS score_sum FROM sc GROUP BY sno) AS b
    ON a.`s#` = b.sno
    WHERE course_num IS NOT NULL

5. 查询「李」姓老师的数量

SELECT COUNT(*) FROM teacher WHERE tname LIKE '李%'

6. 查询学过「张三」老师授课的同学的信息

# 张三老师是01号
SELECT * FROM student WHERE `s#` IN
  (SELECT `s#` FROM sc WHERE `c#` =
    (SELECT `c#` FROM course WHERE `t#` =
      (SELECT `t#` FROM teacher WHERE tname='张三')))

# 7. 查询没有学全所有课程的同学的信息

SELECT `s#`,COUNT(`c#`) AS course_num FROM sc GROUP BY `s#`
    HAVING course_num < (SELECT COUNT(*) FROM course)

# 8. 查询至少有一门课与学号为"01"的同学所学相同的同学的信息

SELECT * FROM student WHERE `s#` IN
  (SELECT DISTINCT `s#` FROM sc WHERE `c#` IN
    (SELECT `c#` FROM sc WHERE `s#`=01))
  AND `s#`!= 01

# 9. 查询和"01"号的同学学习的课程完全相同的其他同学的信息

SELECT `s#` FROM
  (SELECT * FROM sc
    LEFT JOIN
  (SELECT `c#` AS cno FROM sc WHERE `s#` =01) a
    ON sc.`c#` = a.cno) AS b
GROUP BY `s#`
HAVING COUNT(b.`s#`) = (SELECT COUNT(`c#`) AS cno FROM sc WHERE `s#` =01)

# 10. 查询没学过"张三"老师讲授的任一门课程的学生姓名

# 张三是01
# 01老师是教数学,c#是02
SELECT * FROM student WHERE `s#` NOT IN
  (SELECT DISTINCT `s#` FROM sc WHERE `c#` IN
    (SELECT `c#` FROM course WHERE `t#` IN
      (SELECT `t#` FROM teacher WHERE tname = '张三')))

# 11. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

SELECT `s#`, sname, avg_score FROM
  (SELECT `s#`, sname FROM student WHERE `s#` IN
    (SELECT a.`s#` FROM
      (SELECT `s#`,COUNT(`c#`) AS num FROM sc WHERE score <60 GROUP BY `s#`) a
      WHERE num >=2)) AS b
    LEFT JOIN
  (SELECT `s#` AS sno ,AVG(score) AS avg_score FROM sc GROUP BY `s#`) AS c
    ON b.`s#` = c.sno

# 12. 检索" 01 "课程分数小于 60,按分数降序排列的学生信息

SELECT `s#`, sname, score FROM
   student AS a
    LEFT JOIN
  (SELECT `s#` AS sno,`c#`,score FROM sc WHERE `c#`= 01 AND score <60 )b
    ON a.`s#`= b.sno
  WHERE score IS NOT NULL
  ORDER BY score DESC

# 13. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

SELECT `s#` ,AVG(score) AS avg_score FROM sc GROUP BY `s#` ORDER BY avg_score DESC

# 14. 查询各科成绩最高分、最低分和平均分:
# 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
# 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
# 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

SELECT DISTINCT a.`c#`,cname,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 FROM sc a
LEFT JOIN course ON a.`c#`=course.`c#`
LEFT JOIN (SELECT `c#`, MAX(score)最高分, MIN(score)最低分, AVG(score)平均分 FROM sc GROUP BY `c#`)b ON a.`c#`=b.`c#`
LEFT JOIN (SELECT `c#`, ROUND( r1 /cnt * 100, 2 ) AS 及格率 FROM
  (SELECT `c#`, (SUM(CASE WHEN score >=60 THEN 1 ELSE 0 END)*1.00) AS r1 , COUNT(*) AS cnt FROM sc GROUP BY `c#`) c1) c ON a.`c#`=c.`c#`
LEFT JOIN (SELECT `c#`, ROUND( r2 /cnt * 100, 2 ) AS 中等率 FROM
  (SELECT `c#`, (SUM(CASE WHEN score >=70 AND score<80 THEN 1 ELSE 0 END)*1.00) AS r2 , COUNT(*) AS cnt FROM sc GROUP BY `c#`) d1) d ON a.`c#`=d.`c#`
LEFT JOIN (SELECT `c#`, ROUND( r3 /cnt * 100, 2 ) AS 优良率 FROM
  (SELECT `c#`, (SUM(CASE WHEN score >=80 AND score<90 THEN 1 ELSE 0 END)*1.00) AS r3 , COUNT(*) AS cnt FROM sc GROUP BY `c#`) e1) e ON a.`c#`=e.`c#`
LEFT JOIN (SELECT `c#`, ROUND( r4 /cnt * 100, 2 ) AS 优秀率 FROM
  (SELECT `c#`, (SUM(CASE WHEN score >=90 THEN 1 ELSE 0 END)*1.00) AS r4 , COUNT(*) AS cnt FROM sc GROUP BY `c#`) f1) f ON a.`c#`=f.`c#`

# 15. 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺

# mysql中没有rank()函数
# 这种是重复时候保留名次,所以最后名次和人数是一样的
SELECT `s#`, `c#`, score, rank FROM
(SELECT `s#`, `c#`, score,
@currank := IF(@prevrank = score, @currank, @incrank) AS rank,
@incrank := @incrank + 1,
@prevrank := score
FROM sc , (
SELECT @currank :=0, @prevrank := NULL, @incrank := 1
) r
ORDER BY score DESC) s

# 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次

# 这种是当有重复名次的时候变成只有一个名次,所以排名的数量会变少
SELECT `s#`, `c#`, score,
CASE
WHEN @prevrank = score THEN @currank
WHEN @prevrank := score THEN @currank := @currank + 1
END AS rank
FROM sc,
(SELECT @currank :=0, @prevrank := NULL) r
ORDER BY score DESC

# 16.  查询学生的总成绩,并进行排名,总分重复时保留名次空缺

# from后面不需要加表的别名
SELECT `s#`, sum_score, rank FROM
(SELECT `s#`, sum_score,
@currank := IF(@prevrank = sum_score, @currank, @incrank) AS rank,
@incrank := @incrank + 1,
@prevrank := sum_score
FROM
(SELECT `s#`, SUM(score) AS sum_score FROM sc GROUP BY `s#`) c ,
(SELECT @currank :=0, @prevrank := NULL, @incrank := 1) r
ORDER BY sum_score DESC) s

# 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺

SELECT c.*,
CASE
WHEN @prevrank = c.sum_score THEN @currank
WHEN @prevrank := c.sum_score THEN @currank := @currank + 1
END AS rank
FROM
(SELECT a.`s#`,a.sname,SUM(score) AS sum_score
FROM (student AS a RIGHT JOIN sc AS b ON a.`s#` = b.`s#`)
GROUP BY a.`s#` ) c ,
(SELECT @currank := 0 , @prevrank :=NULL ) d
ORDER BY sum_score DESC

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

SELECT a.`c#` , b.cname,
  SUM(CASE WHEN score >=85 AND score <=100 THEN 1 ELSE 0 END ) '[100-85]',
  SUM(CASE WHEN score >=85 AND score <=100 THEN 1 ELSE 0 END )*1.00/COUNT(*) AS '[100-85]percent',
    SUM(CASE WHEN score < 85 AND score >= 70 THEN 1 ELSE 0 END ) '(85-70]',
    SUM(CASE WHEN score < 85 AND score >= 70 THEN 1 ELSE 0 END )*1.00/COUNT(*) AS '(85-70]percent',
    SUM(CASE WHEN score < 70 AND score >= 60 THEN 1 ELSE 0 END ) '(70-60]',
    SUM(CASE WHEN score < 70 AND score >= 60 THEN 1 ELSE 0 END )*1.00/COUNT(*) AS '(85-70]percent',
    SUM(CASE WHEN score < 60 AND score >= 0 THEN 1 ELSE 0 END ) '(60-0]',
    SUM(CASE WHEN score < 60 AND score >= 0 THEN 1 ELSE 0 END )*1.00/COUNT(*) AS '(85-70]percent',
    COUNT(*) AS counts
FROM sc a LEFT JOIN course b ON a.`c#` = b.`c#`
GROUP BY `c#`

# 18. 查询各科成绩前三名的记录

SELECT * FROM sc a WHERE
  (SELECT COUNT(*) FROM sc WHERE `c#`=a.`c#` AND score>a.score)<3
  ORDER BY a.`c#`, a.score DESC;

# 19. 查询每门课程被选修的学生数

SELECT `c#`, COUNT(`s#`) FROM
(SELECT `s#`,`c#` FROM sc ORDER BY `c#`)a
GROUP BY `c#` 

SELECT  a.`c#` , b.cname ,COUNT(*) AS num FROM sc a LEFT JOIN course b ON a.`c#` = b.`c#`
GROUP BY a.`c#`;

# 20. 查询出只选修两门课程的学生学号和姓名

SELECT a.`s#`, a.sname ,cnt FROM
student a
LEFT JOIN
(SELECT `s#`,COUNT(`c#`) AS cnt FROM sc GROUP BY `s#`) b
ON a.`s#`=b.`s#`
WHERE cnt=2

# 21. 查询男生、女生人数

SELECT ssex,COUNT(ssex) FROM student GROUP BY ssex

# 22. 查询名字中含有「风」字的学生信息

SELECT * FROM student WHERE sname LIKE '%风%'

# 23. 查询同名同性学生名单,并统计同名人数

SELECT a.*,b.同名人数 FROM student a
LEFT JOIN (SELECT sname,ssex,COUNT(*) AS 同名人数 FROM student GROUP BY sname,ssex)b
ON a.sname=b.sname AND a.ssex=b.ssex
WHERE b.同名人数>1

# 24. 查询 1990 年出生的学生名单

SELECT * FROM student WHERE YEAR(sage) = 1990

# 25. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

SELECT `c#`, ROUND(AVG(score),2) AS avg_score FROM sc GROUP BY `c#` ORDER BY `c#` ASC

# 26. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩

SELECT c.`s#`,sname ,avg_score FROM
(student c
LEFT JOIN
(SELECT `s#`, avg_score FROM
(SELECT `s#` ,ROUND(AVG(score),2) AS avg_score FROM sc
GROUP BY `s#` ORDER BY avg_score DESC)a
WHERE avg_score >=85) b
ON c.`s#` =b.`s#`)
WHERE avg_score IS NOT NULL

# 27. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数

SELECT a.`s#`,a.sname,b.math, b.score FROM
student a
LEFT JOIN
(SELECT `s#`,`c#` AS math ,score FROM sc WHERE `c#` IN
  (SELECT `c#` FROM course WHERE cname = '数学')
  AND sc.score <60) b
ON a.`s#`=b.`s#`
WHERE b.score IS NOT NULL 

# 28. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)

SELECT a.`s#`,a.`sname`,a.`sage`,a.`ssex`,b.`c#`,b.score FROM
student a LEFT JOIN sc b ON a.`s#` = b.`s#`
LEFT JOIN course c ON c.`c#` = b.`c#`

# 29. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数

SELECT a.`s#`,a.`sname`,a.`sage`,a.`ssex`,b.`c#`,b.score FROM
student a
LEFT JOIN
(SELECT `s#`,`c#`,score FROM sc WHERE score >70) b ON a.`s#`=b.`s#`
LEFT JOIN course c
ON c.`c#`=b.`c#`
WHERE score IS NOT NULL

# 30. 查询不及格的课程

SELECT * FROM sc WHERE score < 60

# 31. 查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名

SELECT a.`s#`, a.sname ,b.score FROM
  student a
    LEFT JOIN
  (SELECT * FROM sc WHERE `c#`='01' AND score >= 80) b
    ON a.`s#` = b.`s#`
  WHERE score IS NOT NULL

# 32. 求每门课程的学生人数

SELECT `c#`,COUNT(`c#`) FROM sc GROUP BY `c#`

# 33. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

SELECT a.`s#`, a.`sname` ,b.`c#`, b.max_score FROM
student a
LEFT JOIN
(SELECT `s#` AS sid,`c#` ,MAX(score) AS max_score FROM sc WHERE `c#` IN
  (SELECT `c#` FROM course WHERE `t#` IN
    (SELECT `t#` FROM teacher WHERE tname = '张三'))) b
ON a.`s#`=b.sid
WHERE max_score IS NOT NULL

# 34. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

SELECT * FROM
(SELECT dd.*,
CASE
WHEN @prevrank = dd.score THEN @currank
WHEN @prevrank := dd.score THEN @currank := @currank + 1
END AS rank
 FROM (SELECT a.*,b.score FROM
student a
LEFT JOIN sc b ON a.`s#` = b.`s#`
LEFT JOIN course c ON b.`c#` = c.`c#`
LEFT JOIN teacher d ON c.`t#` = d.`t#` WHERE d.tname = '张三' ) dd,(SELECT @currank := 0 , @prevrank :=NULL ) ff
ORDER BY score DESC) AS dddddddd
WHERE rank = 1;

# 35. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

SELECT DISTINCT a.`s#`, a.`c#`, a.score FROM sc AS a JOIN sc AS b
WHERE a.`c#` != b.`c#` AND a.score = b.score AND a.`s#` != b.`s#`
ORDER BY a.`s#`, a.`c#`, a.score

# 36. 查询每门功课成绩最好的前两名

# 此题和18题相同
SELECT * FROM sc a WHERE
  (SELECT COUNT(*) FROM sc WHERE `c#`=a.`c#` AND score>a.score)<2
  ORDER BY a.`c#`, a.score DESC;

# 37. 统计每门课程的学生选修人数(超过 5 人的课程才统计)

# 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT a.`c#`, COUNT(*) AS num FROM
course a LEFT JOIN sc b ON a.`c#` = b.`c#`
GROUP BY a.`c#` HAVING num > 5
ORDER BY num,a.`c#`

# 38. 检索至少选修两门课程的学生学号

SELECT DISTINCT`s#`,COUNT(`c#`) AS num FROM sc GROUP BY `s#` HAVING num >=2

# 39. 查询选修了全部课程的学生信息

SELECT * FROM
  (SELECT `s#`,COUNT(*) AS num FROM sc GROUP BY `s#` ) b
  WHERE num = (SELECT COUNT(*) FROM course)

# 40. 查询各学生的年龄,只按年份来算

SELECT *, YEAR(NOW()) - YEAR(sage) AS age FROM student

# 41. 查询本周过生日的学生

SELECT * FROM
(SELECT * , WEEK(sage), MONTH(sage),DAY(sage),
WEEK(STR_TO_DATE(CONCAT_WS(',',YEAR(NOW()),MONTH(sage),DAY(sage)),'%y,%m,%d')) AS w FROM student) a
WHERE w = WEEK(NOW())

# 42. 查询下周过生日的学生

SELECT * FROM
(SELECT * , WEEK(sage), MONTH(sage),DAY(sage),WEEK(NOW()),
WEEK(STR_TO_DATE(CONCAT_WS(',',YEAR(NOW()),MONTH(sage),DAY(sage)),'%y,%m,%d')) AS w FROM student) a
WHERE w + 2 = WEEK(NOW())

# 43. 查询本月过生日的学生

SELECT * , MONTH(sage),MONTH(NOW()) FROM student
WHERE MONTH(sage) = MONTH(NOW())

# 44. 查询下月过生日的学生

SELECT * , MONTH(sage),MONTH(NOW()) FROM student
WHERE MONTH(sage) = MONTH(NOW()) + 1

到此这篇关于最全50个Mysql数据库查询练习题的文章就介绍到这了,更多相关Mysql数据库查询内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Mysql Sql 语句练习题(50道)

    表名和字段 –1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号 –3.教师表 Teacher(t_id,t_name) –教师编号,教师姓名 –4.成绩表 Score(s_id,c_id,s_score) –学生编号,课程编号,分数 测试数据 --建表 --学生表 CREATE TABLE `Student`( `s_

  • 最全50个Mysql数据库查询练习题

    此数据库查询语句是网络上50个数据库查询练习题目,网上有些版本是oracle语句写的,大多数公司还是用免费的mysql数据库,以下都是mysql版本,全部都有验证过. 表名和字段 –1.学生表 Student(s#, sname, sage,ssex) –学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c#,cname,t#) – –课程编号, 课程名称, 教师编号 –3.教师表 Teacher(t#,tname) –教师编号,教师姓名 –4.成绩表 Sc(s#,c#,sc

  • MySQL数据库查询进阶之多表查询详解

    目录 一.多表查询 1.引出 2.笛卡尔积 3. 笛卡尔积的解决方法 二.多表查询分类 1.等值连接和非等值连接 2.自连接和非自连接 3.内连接和外连接 4.UNION 4.自然连接 5.using连接 三.子查询 1.不相关子查询 2.相关子查询 四.聚合函数 1.聚合函数介绍 1.1 AVG和SUM函数 1.2 MIN和MAX函数 1.3 COUNT函数 2.group by 3.使用having进行分组后的筛选 五.where和having的对比 六.select的执行过程 1.关键字顺

  • MySQL数据库查询性能优化的4个技巧干货

    目录 前言 SQL的执行频率 慢查询日志 show profiles详情分析 explain执行计划 1.ID参数 2.select_type参数 3.type参数 前言 MySQL性能优化是一个老生常谈的问题,无论是在实际工作中还是面试中,都不可避免遇到相应的场景,下面博主就总结一些能够帮助大家解决这个问题的小技巧. SQL优化之前需要确认哪些SQL需要优化,这时就需要引起SQL性能分析工具,主要优化的是查询语句. SQL的执行频率 SQL性能优化一般是针对查询语句,所以在定位是否需要优化之前

  • MySQL数据库查询之多表查询总结

    目录 多表关系 表与表之间的联系: 一对多(多对一) 多对多 一对一 多表查询 多表查询的分类 1.连接查询: 2.子查询 内连接 外连接 自连接 联合查询 子查询 标量子查询 列子查询 行子查询 表子查询 多表查询案例 总结 多表关系 在进行数据库表结构的设计时,会根据业务的需求和业务模块之间的关系,分析设计表结构,由于业务之间相互关联,所以各个表结构之间也存在各种联系 表与表之间的联系: 1.一对多(多对一) 2.多对多 3.一对一 一对多(多对一) 例如,一个员工对应一个部门,一个部门可以

  • MYSQL复杂查询练习题以及答案大全(难度适中)

    目录 建库建表插入数据 测试题 答案不唯一,仅供参考 总结 能坚持全部做完的都是高手 直入主题 建库建表插入数据 代码直接按顺序复制就可以 -- 建库 CREATE DATABASE `emp`; -- 打开库 USE emp; -- 建dept表 CREATE TABLE `dept`( `deptno` INT(2) NOT NULL, `dname` VARCHAR(14), `loc` VARCHAR(13), CONSTRAINT pk_dept PRIMARY KEY(deptno)

  • php+mysql数据库查询实例

    本文实例讲述了php+mysql数据库查询的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: <?php      //连接数据库的参数      $host = "localhost";      $user = "root";      $pass = "zq19890319";      $db = "phpdev";      //创建一个mysql连接      $connection =

  • MySql数据库查询中的特殊命令

    第一:   MySQL的安装 下载MySQL软件,修改安装路径之后 安装数据库MySQL5.7.18 第一步:数据库MySQL5.7.18可以在官网上下载对应的版本,下载地址:http://www.filehorse.com/download-mysql-64/, 第二步:将下载好的安装包进行解压到一个盘下面, 第三步:配置环境变量,新建环境变量MySQL_HOME,输入MySQL的安装目录, 然后再把;%MYSQL_HOME%\bin插入到Path的最后面: 第四步:以管理员的身份运行命令行,

  • Python 操作mysql数据库查询之fetchone(), fetchmany(), fetchall()用法示例

    本文实例讲述了Python 操作mysql数据库查询之fetchone(), fetchmany(), fetchall()用法.分享给大家供大家参考,具体如下: demo.py(查询,取出一条数据,fetchone): from pymysql import * def main(): # 创建Connection连接 conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_do

  • MySQL数据库查询性能优化策略

    优化查询 使用Explain语句分析查询语句 Explain 用来分析 SELECT 查询语句,开发人员可以通过分析 Explain 结果来优化查询语句. 通过对查询语句的分析,可以了解查询语句的执行情况,找出查询语句执行的瓶颈,从而优化查询语句. 使用索引查询 MySql中提高性能的一个最有效的方式就是对数据表设计合理的索引. 索引提供了高效访问数据的方法,并且加快查询速度. 如果查询时没有使用索引,那么查询语句将扫描表中所有的记录.在数据量大的时候,这样查询速度会很慢. 使用索引进行查询,查

  • mysql数据库查询基础命令详解

    目录 1.启动数据库命令行客户端 2.查询数据库 3.进入数据库 3.查询所在数据库中所有表信息 4.查询数据库某张表结构 5.简单select查询语句(单张表) 5.1查询单张表所有数据 5.2 LIMIT限制查询结果返回数据项 5.3查询指定列数 5.4 WHERE带条件精准查询 5.5 LIKE使用通配符模糊查询 5.6 ORDER BY查询结果排序 1.启动数据库命令行客户端 #linux命令,注意区分大小写 mysql 2.查询数据库 #执行结果:返回所有数据库列表 SHOW DATA

随机推荐