Search code examples
mysqlmysql-workbenchmysql-error-1064

How to select multiple avg(column) in one select statement in MySql stored procedure?


I find when I select one avg(column) into a variable,it is OK.But when I select more than one avg into a variable,it is wrong.I do not want to write it twice,how to fixed it?

   //work well
  SELECT 
  avg(MathPoint) into  MathPoint 
 from student
 where  sex=1

 SELECT 
  avg(EnglishPoint) into  EnglishPoint
 from student
 where  sex=1

//do not work 
  SELECT  
 avg(EnglishPoint) into  EnglishPoint,
 avg(MathPoint) into  MathPoint,
 from student
 where  sex=1

Solution

  • You have syntax error on INTO usage.

    INTO should follow all variable names together after selecting fields.

    Example:

    SELECT  
           avg(EnglishPoint), avg(MathPoint)
      into EnglishPoint, MathPoint
      from student
     where  sex=1
    

    Documentation: