3

I want to use some argument a few times in same query. How can I do this without duplication of this argument?

This works, but we have to use this argument twice

 connection.query('SELECT ? FROM ?', [usr, usr],...)

I tried this, but it doesn't work (syntax error):

connection.query('SET @param = users; SELECT @param FROM @param',...)
2
  • This kind of problem can be indicative of poor design. These are not the parts of the query which would normally be parametricised Commented May 13, 2016 at 7:33
  • It's just an example. I'll use date as parametr. Eg. ...WHERE date = @param_date Commented May 13, 2016 at 7:38

1 Answer 1

2

For make it work, you have to do to things:

  1. Enable multiple statement queries var connection = mysql.createConnection({multipleStatements: true});

  2. To keep your variables between queries, many times, you must use MySQL transactions.

connection.query('start transaction;set @param=?;select @param;commit',[use])

Read more:

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.