问答

mysql only_full_group_by 以及group by问题

作者:admin 2021-08-02 我要评论

CREATE DATABASE IF NOT EXISTS footprint;use footprint;CREATE TABLE footprint( event_id varchar(50) NOT NULL , student_id varchar(50) NOT NULL , teache...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)
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版本之后,就不行了。

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • mysql only_full_group_by 以及group b

    mysql only_full_group_by 以及group b

  • windows10 无法ping通虚拟机中的docker

    windows10 无法ping通虚拟机中的docker

  • 网站上这种分享是怎么做的?

    网站上这种分享是怎么做的?

  • vscode配置scheme问题

    vscode配置scheme问题

腾讯云代理商
海外云服务器