MySQL 语句练习

Posted 钟爱一根

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL 语句练习相关的知识,希望对你有一定的参考价值。

---恢复内容开始---

在cmd中备份:数据表结构+数据

        mysqldump -u root db1 > d:\\test\\test.sql -p

       数据表结构

        mysqldump -u root -d db1 > d:\\test\\test.sql -p

再cmd中将备份的执行文件写入数据库

      创建表:  create database test default charset utf8 collate utf8_general_ci

      写入:mysql -u root test < d:\\teat\\test.sql -p  同样,加-d也知识写入结构

 

navicat 的而error 1055问题:

 

在MySQL数据库下,执行sql语句,报错(虽然sql也执行成功了,但是看着可不爽)。错误信息如下:

[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column \'information_schema.PROFILING.SEQ\' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

解决方案:

  一、去MySQL的根目录下找my-default.ini配置文件,打开后将sql_mode中的ONLY_FULL_GROUP_BY删掉



  二、执行SELECT @@sql_mode语句



  三、若存在ONLY_FULL_GROUP_BY,将其删除后更新记录,如下



  四、问题解决,但是上述方法只是当前会话的,重新进入MySQL后问题依然存在,彻底解决如下

set sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
View Code

 

表结构数据创建文件如下:

/*
 Navicat Premium Data Transfer

 Source Server         : localhost
 Source Server Type    : MySQL
 Source Server Version : 50624
 Source Host           : localhost
 Source Database       : sqlexam

 Target Server Type    : MySQL
 Target Server Version : 50624
 File Encoding         : utf-8

 Date: 10/21/2016 06:46:46 AM
*/

SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
--  Table structure for `class`
-- ----------------------------
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
  `cid` int(11) NOT NULL AUTO_INCREMENT,
  `caption` varchar(32) NOT NULL,
  PRIMARY KEY (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

-- ----------------------------
--  Records of `class`
-- ----------------------------
BEGIN;
INSERT INTO `class` VALUES (\'1\', \'三年二班\'), (\'2\', \'三年三班\'), (\'3\', \'一年二班\'), (\'4\', \'二年九班\');
COMMIT;

-- ----------------------------
--  Table structure for `course`
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
  `cid` int(11) NOT NULL AUTO_INCREMENT,
  `cname` varchar(32) NOT NULL,
  `teacher_id` int(11) NOT NULL,
  PRIMARY KEY (`cid`),
  KEY `fk_course_teacher` (`teacher_id`),
  CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

-- ----------------------------
--  Records of `course`
-- ----------------------------
BEGIN;
INSERT INTO `course` VALUES (\'1\', \'生物\', \'1\'), (\'2\', \'物理\', \'2\'), (\'3\', \'体育\', \'3\'), (\'4\', \'美术\', \'2\');
COMMIT;

-- ----------------------------
--  Table structure for `score`
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
  `sid` int(11) NOT NULL AUTO_INCREMENT,
  `student_id` int(11) NOT NULL,
  `course_id` int(11) NOT NULL,
  `num` int(11) NOT NULL,
  PRIMARY KEY (`sid`),
  KEY `fk_score_student` (`student_id`),
  KEY `fk_score_course` (`course_id`),
  CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`),
  CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`)
) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8;

-- ----------------------------
--  Records of `score`
-- ----------------------------
BEGIN;
INSERT INTO `score` VALUES (\'1\', \'1\', \'1\', \'10\'), (\'2\', \'1\', \'2\', \'9\'), (\'5\', \'1\', \'4\', \'66\'), (\'6\', \'2\', \'1\', \'8\'), (\'8\', \'2\', \'3\', \'68\'), (\'9\', \'2\', \'4\', \'99\'), (\'10\', \'3\', \'1\', \'77\'), (\'11\', \'3\', \'2\', \'66\'), (\'12\', \'3\', \'3\', \'87\'), (\'13\', \'3\', \'4\', \'99\'), (\'14\', \'4\', \'1\', \'79\'), (\'15\', \'4\', \'2\', \'11\'), (\'16\', \'4\', \'3\', \'67\'), (\'17\', \'4\', \'4\', \'100\'), (\'18\', \'5\', \'1\', \'79\'), (\'19\', \'5\', \'2\', \'11\'), (\'20\', \'5\', \'3\', \'67\'), (\'21\', \'5\', \'4\', \'100\'), (\'22\', \'6\', \'1\', \'9\'), (\'23\', \'6\', \'2\', \'100\'), (\'24\', \'6\', \'3\', \'67\'), (\'25\', \'6\', \'4\', \'100\'), (\'26\', \'7\', \'1\', \'9\'), (\'27\', \'7\', \'2\', \'100\'), (\'28\', \'7\', \'3\', \'67\'), (\'29\', \'7\', \'4\', \'88\'), (\'30\', \'8\', \'1\', \'9\'), (\'31\', \'8\', \'2\', \'100\'), (\'32\', \'8\', \'3\', \'67\'), (\'33\', \'8\', \'4\', \'88\'), (\'34\', \'9\', \'1\', \'91\'), (\'35\', \'9\', \'2\', \'88\'), (\'36\', \'9\', \'3\', \'67\'), (\'37\', \'9\', \'4\', \'22\'), (\'38\', \'10\', \'1\', \'90\'), (\'39\', \'10\', \'2\', \'77\'), (\'40\', \'10\', \'3\', \'43\'), (\'41\', \'10\', \'4\', \'87\'), (\'42\', \'11\', \'1\', \'90\'), (\'43\', \'11\', \'2\', \'77\'), (\'44\', \'11\', \'3\', \'43\'), (\'45\', \'11\', \'4\', \'87\'), (\'46\', \'12\', \'1\', \'90\'), (\'47\', \'12\', \'2\', \'77\'), (\'48\', \'12\', \'3\', \'43\'), (\'49\', \'12\', \'4\', \'87\'), (\'52\', \'13\', \'3\', \'87\');
COMMIT;

-- ----------------------------
--  Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `sid` int(11) NOT NULL AUTO_INCREMENT,
  `gender` char(1) NOT NULL,
  `class_id` int(11) NOT NULL,
  `sname` varchar(32) NOT NULL,
  PRIMARY KEY (`sid`),
  KEY `fk_class` (`class_id`),
  CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;

-- ----------------------------
--  Records of `student`
-- ----------------------------
BEGIN;
INSERT INTO `student` VALUES (\'1\', \'\', \'1\', \'理解\'), (\'2\', \'\', \'1\', \'钢蛋\'), (\'3\', \'\', \'1\', \'张三\'), (\'4\', \'\', \'1\', \'张一\'), (\'5\', \'\', \'1\', \'张二\'), (\'6\', \'\', \'1\', \'张四\'), (\'7\', \'\', \'2\', \'铁锤\'), (\'8\', \'\', \'2\', \'李三\'), (\'9\', \'\', \'2\', \'李一\'), (\'10\', \'\', \'2\', \'李二\'), (\'11\', \'\', \'2\', \'李四\'), (\'12\', \'\', \'3\', \'如花\'), (\'13\', \'\', \'3\', \'刘三\'), (\'14\', \'\', \'3\', \'刘一\'), (\'15\', \'\', \'3\', \'刘二\'), (\'16\', \'\', \'3\', \'刘四\');
COMMIT;

-- ----------------------------
--  Table structure for `teacher`
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
  `tid` int(11) NOT NULL AUTO_INCREMENT,
  `tname` varchar(32) NOT NULL,
  PRIMARY KEY (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

-- ----------------------------
--  Records of `teacher`
-- ----------------------------
BEGIN;
INSERT INTO `teacher` VALUES (\'1\', \'张磊老师\'), (\'2\', \'李平老师\'), (\'3\', \'刘海燕老师\'), (\'4\', \'朱云海老师\'), (\'5\', \'李杰老师\');
COMMIT;

SET FOREIGN_KEY_CHECKS = 1;

表结构和数据
View Code

一、表关系

请创建如下表,并创建相关约束

二、操作表

1、自行创建测试数据   上面有创建代码。

2、查询“生物”课程比“物理”课程成绩高的所有学生的学号;

1 select aa.student_id,aa.num,bb.num from     首先写出要选择的内容,后面想办法
2 (select student_id,num from score left join course on score.course_id=course.cid where cname="生物") as aa     创建临时表,取到生物课程的学号和分数信息
3 left JOIN                                                            连表
4 (select student_id,num from score left join course on score.course_id=course.cid where cname="物理") as bb  再创建临时表,取到物理课程的学号和分数信息
5 on
6 aa.student_id=bb.student_id      按照学号相同进行连接
7 where
8 aa.num>if(isnull(bb.num),0,bb.num)      根据分数进行比较,切记要按照临时表的表明进行槽错,如果学了生物课但是没有选物理课的话,会出现null,则处理null。

3、查询平均成绩大于60分的同学的学号和平均成绩; 

 

1 select student_id,avg(num) from score group by student_id having avg(num)>60   按照学号分组,然后将分数求平均值,因为有聚合函数,所以用having来筛选

注意select 后面的内容必须要和 group by 相照应

 

4、查询所有同学的学号、姓名、选课数、总成绩;

 

1 select student_id,sname,count(course_id),sum(num) from score 
2 left JOIN
3 student        因为要有学生姓名,所以必须连表
4 on score.student_id=student.sid    按照学生id进行连接
5 group by student_id        按照id分组

 

5、查询姓“李”的老师的个数;

 

1 select count(tname) from teacher    
2 where tname like "李%"
3 
4 select count(1) from 
5 (select tid from teacher where tname like \'李%\') as b    必须有这个b,因为这代表了这个临时表的存在,作为临时表供选择的时候必须有as,但是6题中没有as,注意这一点

 

6、查询没学过“李平”老师课的同学的学号、姓名;

 

 1 select sid,sname from student     从学生表中选择学号和姓名
 2 where sid not in            让学号不在  学过李平老师课程的学号名单中
 3 (select student_id from score    从分数表中选择学生id
 4 WHERE score.course_id in        让该学生的课程只要在李平老师的课程列表中就可以了
 5 (select cid from course        以下几行是找到李平老师所教的课程
 6 left JOIN
 7 teacher
 8 on course.teacher_id=teacher.tid
 9 where tname="李平老师")
10 GROUP BY                从分数表中选择学生id,因为有同时选择几门李平老师课程的学生,按照学号分组
11 student_id)          

 

7、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

 

 1 select student_id,sname from
 2 (select student_id from score
 3 where course_id in (1,2)
 4 group by student_id          选过两门课程的人,进行分组按照id
 5 having count(course_id)=2) as aa
 6 left JOIN
 7 student
 8 on aa.student_id=student.sid;
 9  先分组再匹配姓名


先匹配姓名再分组 10 select student_id,sname from 11 (select student_id,course_id from score where course_id = 1 or course_id = 2) as B 学号的来源,当该学生学过两门课程中的一门的时候,作为一个临时表 12 left join student on B.student_id = student.sid group by student_id HAVING count(student_id

以上是关于MySQL 语句练习的主要内容,如果未能解决你的问题,请参考以下文章

mysql基本语句操作练习

MYSQL数据库SQL语句练习实验

MYSQL数据库SQL语句练习实验

MYSQL数据库SQL语句练习实验

MySQL[练习]SQL 语句

MySQL 查询语句练习2