title: How do I use MySQL variables in a SQL statement with Javascript/node.js?
body: <p>I have a MySQL query in javascript running through node that is throwing me an error.</p>
  
  <p>When I print the queryString to the console and paste it in MySQL it works fine but when I print the err in the console it says there is a syntax error.</p>
  
  <p>I think I have narrowed it down but I don't know why it's happening or how to get around it.</p>
  
  <p>Here is the full SQL query:</p>
  
  <pre class="lang-sql prettyprint-override"><code>set @csum := 0;
  set @csum2 := 0;
  set @csum3 := 0;
  SELECT
      r.meeting_date,
      r.user_mode,
      (@csum:=@csum + r.total_profit) AS total_profit,
      (@csum2:=@csum2 + r.ev) AS ev,
      (@csum3:=@csum3 + r.bets) AS bets
  FROM
      (SELECT
          meeting_date,
              user_mode,
              ROUND(SUM(profit)) AS total_profit,
              ROUND(SUM(bsp_profit)) AS total_bsp_profit,
              ROUND(SUM(est_ev)) AS ev,
              SUM(win) AS wins,
              COUNT(race_id) AS bets,
              ROUND(SUM(win) / COUNT(race_id), 2) * 100 AS hit_rate,
              ROUND(SUM(profit) / COUNT(race_id), 2) AS ppr,
              ROUND((SUM(win) / COUNT(race_id)) / (COUNT(race_id) / SUM(num_runners)), 2) AS iv
      FROM
          qs_historic_races
      WHERE
          user_mode = 1
      GROUP BY meeting_date , user_mode) AS r
  ORDER BY meeting_date;
  </code></pre>
  
  <p>If I remove the variables and remove the cumulative sums then it works fine - but the query is no good without them - so removing these rows:</p>
  
  <pre><code>set @csum := 0;
  set @csum2 := 0;
  set @csum3 := 0;
  
  (@csum:=@csum + r.total_profit) AS total_profit,
  (@csum2:=@csum2 + r.ev) AS ev,
  (@csum3:=@csum3 + r.bets) AS bets
  </code></pre>
  
  <p>Here is the entire error that is printed:</p>
  
  <pre><code>0|server   |    '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 \'set @csum2 := 0;\nset @csum3 := 0;\nSELECT\n    r.meeting_date,\n    r.user_mode,\n  \' at line 2',
  0|server   |   sqlState: '42000',
  0|server   |   index: 0,
  0|server   |   sql:
  0|server   |    'set @csum := 0;\nset @csum2 := 0;\nset @csum3 := 0;\nSELECT\n    r.meeting_date,\n    r.user_mode,\n    (@csum:=@csum + r.total_profit) AS total_profit,\n    (@csum2:=@csum2 + r.ev) AS ev,\n    (@csum3:=@csum3 + r.bets) AS bets\nFROM\n    (SELECT\n        meeting_date,\n            user_mode,\n            ROUND(SUM(profit)) AS total_profit,\n            ROUND(SUM(bsp_profit)) AS total_bsp_profit,\n            ROUND(SUM(est_ev)) AS ev,\n            SUM(win) AS wins,\n            COUNT(race_id) AS bets,\n            ROUND(SUM(win) / COUNT(race_id), 2) * 100 AS hit_rate,\n            ROUND(SUM(profit) / COUNT(race_id), 2) AS ppr,\n            ROUND((SUM(win) / COUNT(race_id)) / (COUNT(race_id) / SUM(num_runners)), 2) AS iv\n    FROM\n        qs_historic_races\n    WHERE\n        user_mode = 1\n    GROUP BY meeting_date , user_mode) AS r\nORDER BY meeting_date;' }
  </code></pre>
  
  <p>I've been scratching my head for hours with this so any help is appreciated</p>