创建用户create user 'test1'@'localhost' identified by 'test1';创建用户INSERT INTO mysql.user(Host, User, authentication_string, ssl_cipher, x509_issuer, x509_subject)VALUES ('%', 'test2', 'password', '', '', '');查看用户select host,user,authentication_string from mysql.user;# FLUSH 命令让用户生效FLUSH PRIVILEGES;DROP USER 'test1'@'localhost'; #删除用户'test1'@'localhost'--导出文件 不进入mysqlmysqldump -uroot -p ms05 student > G:\student.sql# 然后输入密码--导入文件mysql -uroot -pcrossfire395612 -Dms05 < G:\student.sqlmysql -uroot -p ms05< G:\student.sqlsource G:\student.sql# student表DROP TABLE IF EXISTS `student`;CREATE TABLE `student` (`stuid` varchar(16) NOT NULL COMMENT '学号',`stunm` varchar(20) NOT NULL COMMENT '学生姓名',PRIMARY KEY (`stuid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- ------------------------------ Records of student-- ----------------------------INSERT INTO `student` VALUES ('1001', '张三');INSERT INTO `student` VALUES ('1002', '李四');INSERT INTO `student` VALUES ('1003', '赵二');INSERT INTO `student` VALUES ('1004', '王五');INSERT INTO `student` VALUES ('1005', '刘青');INSERT INTO `student` VALUES ('1006', '周明');INSERT INTO `student` VALUES ('1007', '吴七');#score表DROP TABLE IF EXISTS `score`;CREATE TABLE `score` (`stuid` varchar(16) NOT NULL COMMENT '学号',`courseno` varchar(20) NOT NULL COMMENT '课程号',`scores` float DEFAULT NULL COMMENT '分数',PRIMARY KEY (`stuid`,`courseno`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- ------------------------------ Records of score-- ----------------------------INSERT INTO `score` VALUES ('1001', 'C001', '67');INSERT INTO `score` VALUES ('1001', 'C002', '87');INSERT INTO `score` VALUES ('1001', 'C003', '83');INSERT INTO `score` VALUES ('1001', 'C004', '88');INSERT INTO `score` VALUES ('1001', 'C005', '77');INSERT INTO `score` VALUES ('1001', 'C006', '77');INSERT INTO `score` VALUES ('1002', 'C001', '68');INSERT INTO `score` VALUES ('1002', 'C002', '88');INSERT INTO `score` VALUES ('1002', 'C003', '84');INSERT INTO `score` VALUES ('1002', 'C004', '89');INSERT INTO `score` VALUES ('1002', 'C005', '78');INSERT INTO `score` VALUES ('1002', 'C006', '78');INSERT INTO `score` VALUES ('1003', 'C001', '69');INSERT INTO `score` VALUES ('1003', 'C002', '89');INSERT INTO `score` VALUES ('1003', 'C003', '85');INSERT INTO `score` VALUES ('1003', 'C004', '90');INSERT INTO `score` VALUES ('1003', 'C005', '79');INSERT INTO `score` VALUES ('1003', 'C006', '79');INSERT INTO `score` VALUES ('1004', 'C001', '70');INSERT INTO `score` VALUES ('1004', 'C002', '90');INSERT INTO `score` VALUES ('1004', 'C003', '86');INSERT INTO `score` VALUES ('1004', 'C004', '91');INSERT INTO `score` VALUES ('1004', 'C005', '80');INSERT INTO `score` VALUES ('1004', 'C006', '80');INSERT INTO `score` VALUES ('1005', 'C001', '71');INSERT INTO `score` VALUES ('1005', 'C002', '91');INSERT INTO `score` VALUES ('1005', 'C003', '87');INSERT INTO `score` VALUES ('1005', 'C004', '92');INSERT INTO `score` VALUES ('1005', 'C005', '81');INSERT INTO `score` VALUES ('1005', 'C006', '81');INSERT INTO `score` VALUES ('1006', 'C001', '72');INSERT INTO `score` VALUES ('1006', 'C002', '92');INSERT INTO `score` VALUES ('1006', 'C003', '88');INSERT INTO `score` VALUES ('1006', 'C004', '93');INSERT INTO `score` VALUES ('1006', 'C005', '82');INSERT INTO `score` VALUES ('1006', 'C006', '82');#course表DROP TABLE IF EXISTS `courses`;CREATE TABLE `courses` (`courseno` varchar(20) NOT NULL COMMENT '课程号',`coursenm` varchar(100) NOT NULL COMMENT '课程姓名',PRIMARY KEY (`courseno`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='课程表';-- ------------------------------ Records of courses-- ----------------------------INSERT INTO `courses` VALUES ('C001', '大学语文');INSERT INTO `courses` VALUES ('C002', '新视野英语');INSERT INTO `courses` VALUES ('C003', '离散数学');INSERT INTO `courses` VALUES ('C004', '概率论与数理统计');INSERT INTO `courses` VALUES ('C005', '线性代数');INSERT INTO `courses` VALUES ('C006', '高等数学(一)');INSERT INTO `courses` VALUES ('C007', '高等数学(二)');
in和exsits
select * from student s where s.stuid in(select stuid from score ss where ss.stuid = s.stuid)select * from student s where EXISTS(select stuid from score ss where ss.stuid = s.stuid)
in: 先执行子查询,把它读到内存当中,然后外查询去对比,外查询的结果集逐一跟in集合判断
exists: 先执行外查询,一一判断
inner join和left join
select * from student sinner join score sc on s.stuid=sc.stuid# 笛卡尔积-- distinctselect distinct s.* from studenr sinner join score sc on s.stuid=sc.stuid-- leftselect distinct s.* from student sleft join score sc on s.stuid=sc.stuid
select s.stuid 学号, s.stunm 学生姓名, GROUP_CONCAT(o.coursenm )课程,GROUP_CONCAT(c.scores) 分数from student sleft join score c on s.stuid=c.stuidleft join courses o on o.courseno=c.coursenogroup by s.stuid,stunm
行转列
-- 方式一select t.stuid 学号, t.stunm 学生姓名,max(c1) 大学语文,max(c2) 新视野英语,max(c3) 离散数学,max(c4) 概率论与数理统计,max(c5) 线性代数,max(c6) 高等数学from (select s.stuid,s.stunm,case courseno when 'C001' then scores end as c1,case courseno when 'C002' then scores end as c2,case courseno when 'C003' then scores end as c3,case courseno when 'C004' then scores end as c4,case courseno when 'C005' then scores end as c5,case courseno when 'C006' then scores end as c6from student sleft join score con s.stuid=c.stuid)as tgroup by t.stuid,t.stunm;-- 方式二select s.stuid 学号,s.stunm 学生姓名,max(case courseno when 'C001' then scores end) 大学语文,max(case courseno when 'C002' then scores end) 新视野英语,max(case courseno when 'C003' then scores end) 离散数学,max(case courseno when 'C004' then scores end) 概率论与数理统计,max(case courseno when 'C005' then scores end) 线性代数,max(case courseno when 'C006' then scores end) 高等数学from student sleft join score con s.stuid=c.stuidgroup by s.stuid,s.stunm;
null为0
ifnull(c1,0) -- 如果为0,值为0--或者加elseselect s.stuid 学号,s.stunm 学生姓名,max(case courseno when 'C001' then scores else 0 end) 大学语文,max(case courseno when 'C002' then scores else 0 end) 新视野英语,max(case courseno when 'C003' then scores else 0 end) 离散数学,max(case courseno when 'C004' then scores else 0 end) 概率论与数理统计,max(case courseno when 'C005' then scores else 0 end) 线性代数,max(case courseno when 'C006' then scores else 0 end) 高等数学from student sleft join score con s.stuid=c.stuidgroup by s.stuid,s.stunm;
行转列来源详解
创建视图
-- 创建视图create view v_ss as select语句
列转行
select t.学号,学生姓名,'大学语文' as 科目,`大学语文` as 分数 from v_ss t union allselect t.学号,学生姓名,'新视野英语' as 科目,`新视野英语` as 分数 from v_ss t union allselect t.学号,学生姓名,'离散数学' as 科目,`离散数学` as 分数 from v_ss t union allselect t.学号,学生姓名,'概率论与数理统计' as 科目,`概率论与数理统计` as 分数 from v_ss t union allselect t.学号,学生姓名,'线性代数' as 科目,`线性代数` as 分数 from v_ss t union allselect t.学号,学生姓名,'高等数学' as 科目,`高等数学` as 分数 from v_ss t;
# 报表开发 学生分数报表 ,要求 展示: 学号、学生姓名,大学语文 , 新视野英语 ,离散数学 ,概率论与数理统计,线性代数,高等数学1 ,高等数学2select s.stuid 学号, s.stunm 学生姓名, GROUP_CONCAT(o.coursenm )课程,GROUP_CONCAT(c.scores) 分数from student sleft join score c on s.stuid=c.stuidleft join courses o on o.courseno=c.coursenogroup by s.stuid,stunm#-------------------------------------------------------------#-- 行转列create view v_ss asselect t.stuid 学号, t.stunm 学生姓名,max(ifnull(c1,0)) 大学语文,max(ifnull(c2,0)) 新视野英语,max(ifnull(c3,0)) 离散数学,max(ifnull(c4,0)) 概率论与数理统计,max(ifnull(c5,0)) 线性代数,max(ifnull(c6,0)) 高等数学from (select s.stuid,s.stunm,case courseno when 'C001' then scores end as c1,case courseno when 'C002' then scores end as c2,case courseno when 'C003' then scores end as c3,case courseno when 'C004' then scores end as c4,case courseno when 'C005' then scores end as c5,case courseno when 'C006' then scores end as c6from student sleft join score con s.stuid=c.stuid)as tgroup by t.stuid,t.stunm;#-------------------------------------------------------------#select s.stuid 学号,s.stunm 学生姓名,max(case courseno when 'C001' then scores end) 大学语文,max(case courseno when 'C002' then scores end) 新视野英语,max(case courseno when 'C003' then scores end) 离散数学,max(case courseno when 'C004' then scores end) 概率论与数理统计,max(case courseno when 'C005' then scores end) 线性代数,max(case courseno when 'C006' then scores end) 高等数学from student sleft join score con s.stuid=c.stuidgroup by s.stuid,s.stunm;select t.stuid as 学号,t.stunm as 姓名,max(ifnull(c1,0)) as 大学语文,max(ifnull(c2,0)) as 新视野英语,max(ifnull(c3,0)) as 离散数学,max(ifnull(c4,0)) as 概率论与数理统计,max(ifnull(c5,0)) as 线性代数,max(ifnull(c6,0)) as 高等数学1,max(ifnull(c7,0)) as 高等数学2from (select s.stuid as stuid, s.stunm as stunm, case when courseno ='C001' then scores end as 'c1', case when courseno ='C002' then scores end as 'c2', case when courseno ='C003' then scores end as 'c3', case when courseno ='C004' then scores end as 'c4', case when courseno ='C005' then scores end as 'c5', case when courseno ='C006' then scores end as 'c6', case when courseno ='C007' then scores end as 'c7'from student sleft join score con s.stuid=c.stuid) as tgroup by t.stuid,t.stunm;---------------------------------------------------------------- 列转行-- SELECT * FROM v_ss;select t.学号,学生姓名,'大学语文' as 科目,`大学语文` as 分数from v_ss t;select t.学号,t.姓名,'大学语文' as 科目 ,`大学语文` as 分数 from v_ss tselect t.学号,学生姓名 from v_ss t;select t.学号,学生姓名,'大学语文' as 科目,`大学语文` as 分数 from v_ss t union allselect t.学号,学生姓名,'新视野英语' as 科目,`新视野英语` as 分数 from v_ss t union allselect t.学号,学生姓名,'离散数学' as 科目,`离散数学` as 分数 from v_ss t union allselect t.学号,学生姓名,'概率论与数理统计' as 科目,`概率论与数理统计` as 分数 from v_ss t union allselect t.学号,学生姓名,'线性代数' as 科目,`线性代数` as 分数 from v_ss t union allselect t.学号,学生姓名,'高等数学' as 科目,`高等数学` as 分数 from v_ss t;
