4

I am looking at some examples in node-mysql https://github.com/felixge/node-mysql

I am confused over when to use ?? and ? as placeholders in constructing a query.

An example is here;

var userId = 1;
var columns = ['username', 'email'];
var query = connection.query('SELECT ?? FROM ?? WHERE id = ?', [columns, 'users', userId], function(err, results) {
  // ...
});

console.log(query.sql); // SELECT `username`, `email` FROM `users` WHERE id = 1

How do I know when to use ?? and when to use ? ?

guagay_wk
  • 26,337
  • 54
  • 186
  • 295
  • 4
    The doc tells you that `??` is for identifiers and `?` for values. [Escaping query values](https://github.com/felixge/node-mysql#escaping-query-values) and [Escaping query identifiers](https://github.com/felixge/node-mysql#escaping-query-identifiers) – t.niese Feb 24 '16 at 11:28

1 Answers1

6

?? is for identifiers. ? is for values.

values are variables. identifiers are contents of variables or constants.

For more details, see links below

https://github.com/felixge/node-mysql#escaping-query-values https://github.com/felixge/node-mysql#escaping-query-identifiers

Credit goes to t.niese's comment.

guagay_wk
  • 26,337
  • 54
  • 186
  • 295
  • 1
    So how does this work in order by. Lets say query - SELECT * FROM blah_table ORDER BY blah_col DESC , the following don't work- SELECT * FROM blah_table ORDER BY ?? ? . I set ?? to blah_col this is fine but DESC doesn't work when setting for ? . – iPhoney Oct 03 '16 at 08:48
  • `values are variables. identifiers are contents of variables or constants` Isn't that the other way around?. – Alexandre Sep 30 '19 at 20:08