Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'learn_mysql.stu_inf.pass_word' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
详细代码如下:
delimiter $$
create procedure p5()
begin
#定义一个变量i,类型是varchar
declare user_name varchar(10);
declare pass_word varchar(10);
#在stu_inf表中查询user_name和pass_word,根据用户名分组查询,查询条件为用户名为zhangsan。将查询的结果赋值给该存储过程中定义的变量——user_name和pass_word。
select stu_inf.user_name,stu_inf.pass_word into user_name,pass_word from stu_inf
group by stu_inf.user_name having stu_inf.user_name='zhangsan';
select user_name,pass_word;
end$$
报错原因:mysql的5.7以上版本都默认sql_mode=only_full_group_by,这个规则实际是为了提升查询的性能,因此规定我们在查询的时候只能准确的查找要查的唯一数据,不支持group by的分组条件与select的条件不一致。
只能写 select id from tableA group id having id='*6*';这种语句,可以在having中控制模糊匹配,但是查询条件和分组条件必须一致。
永久性解决该问题,只需要修改mysql的配置文件,my.cnf文件,添加一行配置
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
保存并退出后重启mysql服务即可
service mysqld restart
或者service mysql restart