1. 创建用户
  2. create user 'test1'@'localhost' identified by 'test1';
  3. 创建用户
  4. INSERT INTO mysql.user(Host, User, authentication_string, ssl_cipher, x509_issuer, x509_subject)
  5. VALUES ('%', 'test2', 'password', '', '', '');
  6. 查看用户
  7. select host,user,authentication_string from mysql.user;
  8. # FLUSH 命令让用户生效
  9. FLUSH PRIVILEGES;
  10. DROP USER 'test1'@'localhost'; #删除用户'test1'@'localhost'
  11. --导出文件 不进入mysql
  12. mysqldump -uroot -p ms05 student > G:\student.sql
  13. # 然后输入密码
  14. --导入文件
  15. mysql -uroot -pcrossfire395612 -Dms05 < G:\student.sql
  16. mysql -uroot -p ms05< G:\student.sql
  17. source G:\student.sql
  18. # student表
  19. DROP TABLE IF EXISTS `student`;
  20. CREATE TABLE `student` (
  21. `stuid` varchar(16) NOT NULL COMMENT '学号',
  22. `stunm` varchar(20) NOT NULL COMMENT '学生姓名',
  23. PRIMARY KEY (`stuid`)
  24. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  25. -- ----------------------------
  26. -- Records of student
  27. -- ----------------------------
  28. INSERT INTO `student` VALUES ('1001', '张三');
  29. INSERT INTO `student` VALUES ('1002', '李四');
  30. INSERT INTO `student` VALUES ('1003', '赵二');
  31. INSERT INTO `student` VALUES ('1004', '王五');
  32. INSERT INTO `student` VALUES ('1005', '刘青');
  33. INSERT INTO `student` VALUES ('1006', '周明');
  34. INSERT INTO `student` VALUES ('1007', '吴七');
  35. #score表
  36. DROP TABLE IF EXISTS `score`;
  37. CREATE TABLE `score` (
  38. `stuid` varchar(16) NOT NULL COMMENT '学号',
  39. `courseno` varchar(20) NOT NULL COMMENT '课程号',
  40. `scores` float DEFAULT NULL COMMENT '分数',
  41. PRIMARY KEY (`stuid`,`courseno`)
  42. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  43. -- ----------------------------
  44. -- Records of score
  45. -- ----------------------------
  46. INSERT INTO `score` VALUES ('1001', 'C001', '67');
  47. INSERT INTO `score` VALUES ('1001', 'C002', '87');
  48. INSERT INTO `score` VALUES ('1001', 'C003', '83');
  49. INSERT INTO `score` VALUES ('1001', 'C004', '88');
  50. INSERT INTO `score` VALUES ('1001', 'C005', '77');
  51. INSERT INTO `score` VALUES ('1001', 'C006', '77');
  52. INSERT INTO `score` VALUES ('1002', 'C001', '68');
  53. INSERT INTO `score` VALUES ('1002', 'C002', '88');
  54. INSERT INTO `score` VALUES ('1002', 'C003', '84');
  55. INSERT INTO `score` VALUES ('1002', 'C004', '89');
  56. INSERT INTO `score` VALUES ('1002', 'C005', '78');
  57. INSERT INTO `score` VALUES ('1002', 'C006', '78');
  58. INSERT INTO `score` VALUES ('1003', 'C001', '69');
  59. INSERT INTO `score` VALUES ('1003', 'C002', '89');
  60. INSERT INTO `score` VALUES ('1003', 'C003', '85');
  61. INSERT INTO `score` VALUES ('1003', 'C004', '90');
  62. INSERT INTO `score` VALUES ('1003', 'C005', '79');
  63. INSERT INTO `score` VALUES ('1003', 'C006', '79');
  64. INSERT INTO `score` VALUES ('1004', 'C001', '70');
  65. INSERT INTO `score` VALUES ('1004', 'C002', '90');
  66. INSERT INTO `score` VALUES ('1004', 'C003', '86');
  67. INSERT INTO `score` VALUES ('1004', 'C004', '91');
  68. INSERT INTO `score` VALUES ('1004', 'C005', '80');
  69. INSERT INTO `score` VALUES ('1004', 'C006', '80');
  70. INSERT INTO `score` VALUES ('1005', 'C001', '71');
  71. INSERT INTO `score` VALUES ('1005', 'C002', '91');
  72. INSERT INTO `score` VALUES ('1005', 'C003', '87');
  73. INSERT INTO `score` VALUES ('1005', 'C004', '92');
  74. INSERT INTO `score` VALUES ('1005', 'C005', '81');
  75. INSERT INTO `score` VALUES ('1005', 'C006', '81');
  76. INSERT INTO `score` VALUES ('1006', 'C001', '72');
  77. INSERT INTO `score` VALUES ('1006', 'C002', '92');
  78. INSERT INTO `score` VALUES ('1006', 'C003', '88');
  79. INSERT INTO `score` VALUES ('1006', 'C004', '93');
  80. INSERT INTO `score` VALUES ('1006', 'C005', '82');
  81. INSERT INTO `score` VALUES ('1006', 'C006', '82');
  82. #course表
  83. DROP TABLE IF EXISTS `courses`;
  84. CREATE TABLE `courses` (
  85. `courseno` varchar(20) NOT NULL COMMENT '课程号',
  86. `coursenm` varchar(100) NOT NULL COMMENT '课程姓名',
  87. PRIMARY KEY (`courseno`)
  88. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='课程表';
  89. -- ----------------------------
  90. -- Records of courses
  91. -- ----------------------------
  92. INSERT INTO `courses` VALUES ('C001', '大学语文');
  93. INSERT INTO `courses` VALUES ('C002', '新视野英语');
  94. INSERT INTO `courses` VALUES ('C003', '离散数学');
  95. INSERT INTO `courses` VALUES ('C004', '概率论与数理统计');
  96. INSERT INTO `courses` VALUES ('C005', '线性代数');
  97. INSERT INTO `courses` VALUES ('C006', '高等数学(一)');
  98. INSERT INTO `courses` VALUES ('C007', '高等数学(二)');

in和exsits

  1. select * from student s where s.stuid in(select stuid from score ss where ss.stuid = s.stuid)
  2. select * from student s where EXISTS(select stuid from score ss where ss.stuid = s.stuid)

in: 先执行子查询,把它读到内存当中,然后外查询去对比,外查询的结果集逐一跟in集合判断
exists: 先执行外查询,一一判断
image.png

inner join和left join

  1. select * from student s
  2. inner join score sc on s.stuid=sc.stuid
  3. # 笛卡尔积
  4. -- distinct
  5. select distinct s.* from studenr s
  6. inner join score sc on s.stuid=sc.stuid
  7. -- left
  8. select distinct s.* from student s
  9. left join score sc on s.stuid=sc.stuid
  1. select s.stuid 学号, s.stunm 学生姓名, GROUP_CONCAT(o.coursenm )课程,GROUP_CONCAT(c.scores) 分数
  2. from student s
  3. left join score c on s.stuid=c.stuid
  4. left join courses o on o.courseno=c.courseno
  5. group by s.stuid,stunm

行转列

  1. -- 方式一
  2. select t.stuid 学号, t.stunm 学生姓名
  3. ,max(c1) 大学语文
  4. ,max(c2) 新视野英语
  5. ,max(c3) 离散数学
  6. ,max(c4) 概率论与数理统计
  7. ,max(c5) 线性代数
  8. ,max(c6) 高等数学
  9. from (
  10. select s.stuid
  11. ,s.stunm
  12. ,case courseno when 'C001' then scores end as c1
  13. ,case courseno when 'C002' then scores end as c2
  14. ,case courseno when 'C003' then scores end as c3
  15. ,case courseno when 'C004' then scores end as c4
  16. ,case courseno when 'C005' then scores end as c5
  17. ,case courseno when 'C006' then scores end as c6
  18. from student s
  19. left join score c
  20. on s.stuid=c.stuid
  21. )as t
  22. group by t.stuid,t.stunm;
  23. -- 方式二
  24. select s.stuid 学号
  25. ,s.stunm 学生姓名
  26. ,max(case courseno when 'C001' then scores end) 大学语文
  27. ,max(case courseno when 'C002' then scores end) 新视野英语
  28. ,max(case courseno when 'C003' then scores end) 离散数学
  29. ,max(case courseno when 'C004' then scores end) 概率论与数理统计
  30. ,max(case courseno when 'C005' then scores end) 线性代数
  31. ,max(case courseno when 'C006' then scores end) 高等数学
  32. from student s
  33. left join score c
  34. on s.stuid=c.stuid
  35. group by s.stuid,s.stunm;

null为0

  1. ifnull(c1,0) -- 如果为0,值为0
  2. --或者加else
  3. select s.stuid 学号
  4. ,s.stunm 学生姓名
  5. ,max(case courseno when 'C001' then scores else 0 end) 大学语文
  6. ,max(case courseno when 'C002' then scores else 0 end) 新视野英语
  7. ,max(case courseno when 'C003' then scores else 0 end) 离散数学
  8. ,max(case courseno when 'C004' then scores else 0 end) 概率论与数理统计
  9. ,max(case courseno when 'C005' then scores else 0 end) 线性代数
  10. ,max(case courseno when 'C006' then scores else 0 end) 高等数学
  11. from student s
  12. left join score c
  13. on s.stuid=c.stuid
  14. group by s.stuid,s.stunm;

行转列来源详解

行转列

创建视图

  1. -- 创建视图
  2. create view v_ss as select语句

列转行

  1. select t.学号,学生姓名,'大学语文' as 科目,`大学语文` as 分数 from v_ss t union all
  2. select t.学号,学生姓名,'新视野英语' as 科目,`新视野英语` as 分数 from v_ss t union all
  3. select t.学号,学生姓名,'离散数学' as 科目,`离散数学` as 分数 from v_ss t union all
  4. select t.学号,学生姓名,'概率论与数理统计' as 科目,`概率论与数理统计` as 分数 from v_ss t union all
  5. select t.学号,学生姓名,'线性代数' as 科目,`线性代数` as 分数 from v_ss t union all
  6. select t.学号,学生姓名,'高等数学' as 科目,`高等数学` as 分数 from v_ss t;
  1. # 报表开发 学生分数报表 ,要求 展示: 学号、学生姓名,大学语文 , 新视野英语 ,离散数学 ,概率论与数理统计,线性代数,高等数学1 ,高等数学2
  2. select s.stuid 学号, s.stunm 学生姓名, GROUP_CONCAT(o.coursenm )课程,GROUP_CONCAT(c.scores) 分数
  3. from student s
  4. left join score c on s.stuid=c.stuid
  5. left join courses o on o.courseno=c.courseno
  6. group by s.stuid,stunm
  7. #-------------------------------------------------------------#
  8. -- 行转列
  9. create view v_ss as
  10. select t.stuid 学号, t.stunm 学生姓名
  11. ,max(ifnull(c1,0)) 大学语文
  12. ,max(ifnull(c2,0)) 新视野英语
  13. ,max(ifnull(c3,0)) 离散数学
  14. ,max(ifnull(c4,0)) 概率论与数理统计
  15. ,max(ifnull(c5,0)) 线性代数
  16. ,max(ifnull(c6,0)) 高等数学
  17. from (
  18. select s.stuid
  19. ,s.stunm
  20. ,case courseno when 'C001' then scores end as c1
  21. ,case courseno when 'C002' then scores end as c2
  22. ,case courseno when 'C003' then scores end as c3
  23. ,case courseno when 'C004' then scores end as c4
  24. ,case courseno when 'C005' then scores end as c5
  25. ,case courseno when 'C006' then scores end as c6
  26. from student s
  27. left join score c
  28. on s.stuid=c.stuid
  29. )as t
  30. group by t.stuid,t.stunm;
  31. #-------------------------------------------------------------#
  32. select s.stuid 学号
  33. ,s.stunm 学生姓名
  34. ,max(case courseno when 'C001' then scores end) 大学语文
  35. ,max(case courseno when 'C002' then scores end) 新视野英语
  36. ,max(case courseno when 'C003' then scores end) 离散数学
  37. ,max(case courseno when 'C004' then scores end) 概率论与数理统计
  38. ,max(case courseno when 'C005' then scores end) 线性代数
  39. ,max(case courseno when 'C006' then scores end) 高等数学
  40. from student s
  41. left join score c
  42. on s.stuid=c.stuid
  43. group by s.stuid,s.stunm;
  44. select t.stuid as 学号
  45. ,t.stunm as 姓名
  46. ,max(ifnull(c1,0)) as 大学语文
  47. ,max(ifnull(c2,0)) as 新视野英语
  48. ,max(ifnull(c3,0)) as 离散数学
  49. ,max(ifnull(c4,0)) as 概率论与数理统计
  50. ,max(ifnull(c5,0)) as 线性代数
  51. ,max(ifnull(c6,0)) as 高等数学1
  52. ,max(ifnull(c7,0)) as 高等数学2
  53. from (
  54. select s.stuid as stuid
  55. , s.stunm as stunm
  56. , case when courseno ='C001' then scores end as 'c1'
  57. , case when courseno ='C002' then scores end as 'c2'
  58. , case when courseno ='C003' then scores end as 'c3'
  59. , case when courseno ='C004' then scores end as 'c4'
  60. , case when courseno ='C005' then scores end as 'c5'
  61. , case when courseno ='C006' then scores end as 'c6'
  62. , case when courseno ='C007' then scores end as 'c7'
  63. from student s
  64. left join score c
  65. on s.stuid=c.stuid
  66. ) as t
  67. group by t.stuid
  68. ,t.stunm
  69. ;
  70. --------------------------------------------------------------
  71. -- 列转行
  72. -- SELECT * FROM v_ss;
  73. select t.学号,学生姓名,'大学语文' as 科目,`大学语文` as 分数from v_ss t;
  74. select t.学号,t.姓名,'大学语文' as 科目 ,`大学语文` as 分数 from v_ss t
  75. select t.学号,学生姓名 from v_ss t;
  76. select t.学号,学生姓名,'大学语文' as 科目,`大学语文` as 分数 from v_ss t union all
  77. select t.学号,学生姓名,'新视野英语' as 科目,`新视野英语` as 分数 from v_ss t union all
  78. select t.学号,学生姓名,'离散数学' as 科目,`离散数学` as 分数 from v_ss t union all
  79. select t.学号,学生姓名,'概率论与数理统计' as 科目,`概率论与数理统计` as 分数 from v_ss t union all
  80. select t.学号,学生姓名,'线性代数' as 科目,`线性代数` as 分数 from v_ss t union all
  81. select t.学号,学生姓名,'高等数学' as 科目,`高等数学` as 分数 from v_ss t;