mardi 21 mai 2019

how to use IF EXISTS statement in node mysql?

I am using NodeJs mysql module, and I want to check account existence before actually querying the password, here is the sql:

let sql = `IF EXISTS (SELECT name FROM users WHERE name='${data.name}') SELECT password FROM users WHERE name='${data.name}'`;

It works fine when no if statement, and once add the IF EXISTS part in it, no matter what I adjust, always get the error like:

code: 'ER_PARSE_ERROR',

errno: 1064,

sqlMessage: '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...,

sqlState: '42000'

I will appreciate it if anyone can help me!

Aucun commentaire:

Enregistrer un commentaire