0

So, I am selecting entries from a table and grouping them, but also want to create an index column in the result. Executing this query in MySQL Workbench works, but when I try to execute it on the node.js server there is a parse error and mysql says that the syntax is incorrect.

I am using a raw query and QueryChainer from sequelize.

SET @cnt = 0;
SELECT (@cnt := @cnt + 1) AS id, et.fileName, et.creationDate,
  SUM(COALESCE(et.amountExported, 0)) AS sumExported, 
  SUM(COALESCE(et.amountImported, 0)) AS sumImported,
  (SUM(COALESCE(et.amountExported, 0)) - SUM(COALESCE(et.amountImported, 0))) AS difference
FROM exported_transactions AS et
WHERE (et.creationDate BETWEEN ? AND ?) /* ? is replaced by a date string */
GROUP BY et.fileName

Removing the variable in the sql query and executing it on the node.js server works fine:

SELECT et.fileName, et.creationDate,
  SUM(COALESCE(et.amountExported, 0)) AS sumExported, 
  SUM(COALESCE(et.amountImported, 0)) AS sumImported,
  (SUM(COALESCE(et.amountExported, 0)) - SUM(COALESCE(et.amountImported, 0))) AS difference
FROM exported_transactions AS et
WHERE (et.creationDate BETWEEN ? AND ?) /* ? is replaced by a date string */
GROUP BY et.fileName

Of course, I can do just fine without the "id" column, but I want it there for sorting. Is there a solution to my problem or is there some other way to generate this auto incremented column?

1 Answer 1

1

Have you set multipleStatements: true in your database setup object?

If not, remember that with multiple statements the database query passes an array of results (instead of a single results object) to the callback for the query. The "SET" statement will have its own result object in results[0], even though it's not returning any interesting data.

Sign up to request clarification or add additional context in comments.

Comments

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.