Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘learn_mysq

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

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值