mysql max 与 where 间的执行问题
执行sql:
CREATE TABLE `grades` ( `id` int(20) NOT NULL AUTO_INCREMENT, `student_id` int(20) NOT NULL, `subject` varchar(20) COLLATE utf8_bin DEFAULT NULL, `grades` varchar(20) COLLATE utf8_bin DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; insert into `grades`(`id`,`student_id`,`subject`,`grades`) values (1,1,'语文','80'),(2,1,'数学','89'),(3,2,'语文','90'); CREATE TABLE `student` ( `id` int(20) NOT NULL AUTO_INCREMENT, `name` varchar(20) COLLATE utf8_bin DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; insert into `student`(`id`,`name`) values (1,'xiaoming'),(2,'xiaohong'),(3,'xiaobai');
执行结果:
执行结果1:
执行结果2:
执行结果3:
执行结果4:
执行结果5:
由此可见:
1.max是在where条件之前执行的,
2.而group by 又是在max之前执行的
3.当where 与 group by 同时出现 优先执行where条件