Update multiple rows in a table based on result from subquery

Adam picture Adam · Nov 4, 2012 · Viewed 7.2k times · Source

I have the following query:

SELECT 
  stat.mcq_id, 
  ROUND( stat.total_score / stat.num_taken, 2 ) AS avg_score
    FROM (
      SELECT 
        user_mcq.mcq_id, 
        SUM( score ) AS total_score, 
        COUNT( user_mcq.id ) AS num_taken
      FROM user_mcq
      INNER JOIN user ON ( user.id = user_mcq.user_id ) 
      WHERE user.level_id =3
      AND user_mcq.is_complete =1
      GROUP BY user_mcq.mcq_id
    ) AS stat

This produces:

mcq_id  avg_score
1       5.75
2       9.22
6       8.81
7       8.94
14      7.00
16      9.46

I would like to use this to update another table called mcq using mcq_id from the result to match against mcq.id

I've tried the following, but without success:

UPDATE mcq SET mcq.avg_score = stats.avg_score FROM (
  SELECT 
  stat.mcq_id, 
  ROUND( stat.total_score / stat.num_taken, 2 ) AS avg_score
    FROM (
      SELECT 
        user_mcq.mcq_id, 
        SUM( score ) AS total_score, 
        COUNT( user_mcq.id ) AS num_taken
      FROM user_mcq
      INNER JOIN user ON ( user.id = user_mcq.user_id ) 
      WHERE user.level_id =3
      AND user_mcq.is_complete =1
      GROUP BY user_mcq.mcq_id
    ) AS stat
) AS stats
WHERE mcq.id = stats.mcq_id;

This gives:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM ( SELECT stat.mcq_id, ROUND( stat.total_score / stat.num_taken, 2 ) A' at line 1

Answer

Yogendra Singh picture Yogendra Singh · Nov 4, 2012

I think you can use the join with the able table and update the column as below:

 UPDATE mcq, 
        (SELECT 
            stat.mcq_id, 
            ROUND( stat.total_score / stat.num_taken, 2 ) AS avg_score
             FROM (SELECT 
                  user_mcq.mcq_id, 
                  SUM(score ) AS total_score, 
                  COUNT( user_mcq.id ) AS num_taken
                  FROM user_mcq
                  INNER JOIN user ON ( user.id = user_mcq.user_id ) 
                  WHERE user.level_id =3
                  AND user_mcq.is_complete =1
                  GROUP BY user_mcq.mcq_id
             ) AS stat
       ) AS stats
SET mcq.avg_score = stats.avg_score 
WHERE mcq.mcq_id = stats.mcq_id;