mysql only_full_group_by 以及group by问题

CREATE DATABASE IF NOT EXISTS  footprint;
use footprint;
CREATE TABLE footprint(
    event_id varchar(50) NOT NULL ,
    student_id varchar(50) NOT NULL ,
    teacher_id varchar(50) NOT NULL ,
    course_id varchar(50) NOT NULL ,
    time datetime(3) NOT NULL ,
    primary key(event_id)
) ;
INSERT INTO `footprint` VALUES ('1', 'student1', 'teacher1', 'course1', '2020-5-30 8:00:00');
INSERT INTO `footprint` VALUES ('2', 'student1', 'teacher1', 'course1', '2020-5-30 12:00:00');
INSERT INTO `footprint` VALUES ('3', 'student1', 'teacher1', 'course1', '2020-5-30 22:00:00');
INSERT INTO `footprint` VALUES ('4', 'student1', 'teacher1', 'course2', '2020-5-30 15:00:00');
INSERT INTO `footprint` VALUES ('5', 'student1', 'teacher1', 'course2', '2020-5-30 16:00:00');
INSERT INTO `footprint` VALUES ('6', 'student1', 'teacher1', 'course3', '2020-5-30 17:00:00');
INSERT INTO `footprint` VALUES ('7', 'student1', 'teacher1', 'course4', '2020-5-30 20:00:00');
INSERT INTO `footprint` VALUES ('8', 'student1', 'teacher1', 'course5', '2020-5-30 21:00:00');
INSERT INTO `footprint` VALUES ('9', 'student1', 'teacher1', 'course5', '2020-5-30 21:00:00');
INSERT INTO `footprint` VALUES ('10', 'student1', 'teacher1', 'course6', '2020-5-29 21:00:00');
INSERT INTO `footprint` VALUES ('11', 'student1', 'teacher1', 'course6', '2020-5-31 21:00:00');

teacher_id教师,有多个course_id课程,但是每一个course_id课程都是唯一的,并且唯一对应一个teacher_id。不存在一个course_id课程对应多个teacher_id教师的情况。

记录学生,在具体什么时间,查看过什么课程。最终返回给前端展示的:按照时间逆序输出的不重复的课程。

实验环境:mysql5.7.27、tidb 5.7.25-TiDB-v3.0.5。

以下两个问题,谷歌半天,都没有结果,求助。

问题一:
mysql:对于only_full_group_by,有这个模式的时候,对于数据库表中实际有的表,会有效果,无法执行;那么对于执行过程中间产生的虚拟表(中间表),也有效。
tidb:实际表footprint,对于only_full_group_by有效;但是中间虚拟表tem,就没有起作用,下面的语句依然可以执行

SELECT  group_concat(event_id), student_id, teacher_id, course_id, group_concat(time) from (select * from footprint where student_id='student1'  order  by  time  desc ) tem group  by tem.course_id limit  0, 10 ;

意思就是说,这条SQL语句,在mysql中开启了only_full_group_by,执行会失败;但是在tidb中执行OK。

问题二:

SELECT  group_concat(event_id), student_id, teacher_id, course_id, group_concat(time) from (select * from footprint where student_id='student1'  order  by  time  desc ) tem group  by tem.course_id order  by  time  desc  limit  0, 10 ;

group by去重后,选择的记录,是按照什么规则选的?
多条重复记录在同一个组里面,那么选择出一条记录出来,按照什么规则选的呢?

https://bugs.mysql.com/bug.ph... , This is a change of behavior introduced in 5.7.

在5.7.5版本之前,这条语句还是可以按照需要正常返回的,但是5.7.5版本之后,就不行了。

阅读 1.8k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题