5

I'm trying to run mysql update query with select in it , but I'm getting an error. Query is this:

UPDATE keywords_stats_google_temp SET (Impressions_chg, Clicks_chg, AveragePosition_chg, Ctr_chg, AverageCpc_chg, CurrentMaxCpc_chg, FreeJoins_chg, PaidJoins_chg) = (SELECT 
                SUM(Impressions) AS Impressions,
                SUM(Clicks) AS Clicks,
                SUM(Impressions*AveragePosition)/SUM(Impressions) AS AveragePosition,
                (SUM(Clicks)*revenue_price)/SUM(Impressions) AS Ctr,
                SUM(Spend)/SUM(Clicks) AS AverageCpc,
                CurrentMaxCpc,
                SUM(free_joins) AS FreeJoins,
                SUM(paid_joins) AS PaidJoins
                FROM (SELECT KeywordId FROM keywords_stats_google_temp) a JOIN keywords_stats_google_naughtymeetings b ON b.KeywordId = a.KeywordId WHERE b.TimePeriod >= '2012-04-01 00:00:00'
                AND b.TimePeriod <= '2012-04-23 00:00:00' GROUP BY a.KeywordId, MatchType) 

But I'm getting only "#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 '(Impressions_chg, Clicks_chg, AveragePosition_chg, Ctr_chg, AverageCpc_chg, Curr' at line 1"

Can anybody help me with this?

Thanks!

8
  • You can't have SET (a,b) = (value_a, value_b) in MySQL. Commented Jun 6, 2012 at 10:48
  • Actually I checked an article and there is such a way: karlssonondatabases.blogspot.com/2009/01/… Commented Jun 6, 2012 at 10:53
  • Yes, it's possible. But not with this syntax. You'll have to rewrite the statement using MySQL syntax for UPDATE. Commented Jun 6, 2012 at 10:56
  • Can you give me some more info about that? Thx Commented Jun 6, 2012 at 11:01
  • What is confusing is the MatchType in the GROUP BY. Is there a relevant field in the table to be updated? Commented Jun 6, 2012 at 11:03

1 Answer 1

9

You can't have SET (a,b) = (value_a, value_b) in MySQL.

Rewrite the query. Something like this:

UPDATE 
    keywords_stats_google_temp AS u
  JOIN
    ( SELECT 
          SUM(Impressions) AS Impressions,
          SUM(Clicks) AS Clicks,
          SUM(Impressions*AveragePosition)/SUM(Impressions) AS AveragePosition,
          (SUM(Clicks)*revenue_price) / SUM(Impressions) AS Ctr,
          SUM(Spend)/SUM(Clicks) AS AverageCpc,
          CurrentMaxCpc,
          SUM(free_joins) AS FreeJoins,
          SUM(paid_joins) AS PaidJoins
      FROM keywords_stats_google_naughtymeetings AS b  
      WHERE b.TimePeriod >= '2012-04-01 00:00:00'
        AND b.TimePeriod <= '2012-04-23 00:00:00' 
      GROUP BY KeywordId, 
               MatchType
    ) AS tmp
        ON  tmp.KeywordId = u.KeywordId 
        AND tmp.MatchType = u.MatchType
SET 
  u.Impressions_chg     = tmp.Impressions, 
  u.Clicks_chg          = tmp.Clicks, 
  u.AveragePosition_chg = tmp.AveragePosition, 
  u.Ctr_chg             = tmp.Ctr,
  u.AverageCpc_chg      = tmp.AverageCpc, 
  u.CurrentMaxCpc_chg   = tmp.CurrentMaxCpc, 
  u.FreeJoins_chg       = tmp.FreeJoins, 
  u.PaidJoins_chg       = tmp.PaidJoins ;
Sign up to request clarification or add additional context in comments.

4 Comments

I run this query and it looks like it's working, but it's very very slow. Probably there should be away to optimize this ...
Compound index on (KeywordId, MatchType) would help on table u. Not sure about the other.
First test that the subquery (with the GROUP BY) calculates what it should calculate. After you are sure, then try to optimize the UPDATE.
@pocko If this solution worked for you, please consider marking it as the Accepted Answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.