一、代码:
drop procedure if exists a;
create procedure a()
begin
declare i int ;
set i=100;
while i<1000 do
insert into user(id,name,password,apply_time,last_login_time,gender,email,ip) values(max(id)+1,concat('infc',i),concat('infc',i,'@qq.com'),now(),now(),'0',concat('infc',i,'@qq.com') , '192\.168\.1\.111');
set i=i+1;
end while;
end;
call a();
create procedure a()
begin
declare i int ;
set i=100;
while i<1000 do
insert into user(id,name,password,apply_time,last_login_time,gender,email,ip) values(max(id)+1,concat('infc',i),concat('infc',i,'@qq.com'),now(),now(),'0',concat('infc',i,'@qq.com') , '192\.168\.1\.111');
set i=i+1;
end while;
end;
call a();
二、运行结果:
[SQL] drop procedure if exists a;
Affected rows: 0
Time: 0.000ms
Affected rows: 0
Time: 0.000ms
[SQL]
create procedure a()
begin
declare i int ;
set i=100;
while i<1000 do
insert into user(id,name,password,apply_time,last_login_time,gender,email,ip) values(max(id)+1,concat('infc',i),concat('infc',i,'@qq.com'),now(),now(),'0',concat('infc',i,'@qq.com') , '192\.168\.1\.111');
set i=i+1;
end while;
end;
Affected rows: 0
Time: 0.000ms
create procedure a()
begin
declare i int ;
set i=100;
while i<1000 do
insert into user(id,name,password,apply_time,last_login_time,gender,email,ip) values(max(id)+1,concat('infc',i),concat('infc',i,'@qq.com'),now(),now(),'0',concat('infc',i,'@qq.com') , '192\.168\.1\.111');
set i=i+1;
end while;
end;
Affected rows: 0
Time: 0.000ms
[SQL]
call a();
[Err] 1111 - Invalid use of group function
call a();
[Err] 1111 - Invalid use of group function
三、分析:
1.drop procedure if exists a;未出现语法错误
2.问题可能出现在SQL语句:insert into user(id,name,password,apply_time,last_login_time,gender,email,ip) values(max(id)+1,concat('infc',i),concat('infc',i,'@qq.com'),now(),now(),'0',concat('infc',i,'@qq.com') , '192\.168\.1\.111');
发现是由于在此处由于用到了max(id)+1的原因。由于没有进行更深层次的分析,不好枉下结论。但可以借鉴出现了相同的报错信息的问题SQL语句,讲解如下。
三、类似问题(转载):
The thing about aggregate functions such as COUNT, MAX, etc., is that they are not actually calculated until the rest of the query is evaluated. For example, when you want to select the COUNT of all the rows in a table, MySQL will first compile all the rows from the table and then find the count.
This can cause some problems. For example, what if you only want to return results where the COUNT is greater than 3? Or expressed in code:
The problem here is that MySQL doesn't calculate the value of `count` until it compiles the entire result set. In other words, MySQL won't calculate `count` until it finds all rows where `count` is greater than three. Oops.
The solution to the above problem is to use the HAVING keyword, which is a special WHERE that gets applied after the initial result set gets calculated:
Or at least that's what "Invalid use of group function" means.
Of course, the problem that you were hitting was a little different. Simply put, you have to use a column name, instead of a calculated value, in the ORDER BY clause.
This can cause some problems. For example, what if you only want to return results where the COUNT is greater than 3? Or expressed in code:
Expand|Select|Wrap|Line Numbers
- SELECT
- `name`,
- COUNT(`id`)
- AS `count`
- FROM
- `Table`
- WHERE
- `count` > 3
- GROUP BY
- `name`;
The solution to the above problem is to use the HAVING keyword, which is a special WHERE that gets applied after the initial result set gets calculated:
Expand|Select|Wrap|Line Numbers
- SELECT
- `name`,
- COUNT(`id`)
- AS `count`
- FROM
- `Table`
- GROUP BY
- `name`
- HAVING
- `count` > 3;
Of course, the problem that you were hitting was a little different. Simply put, you have to use a column name, instead of a calculated value, in the ORDER BY clause.