How to use mysql Joins with paramerized query in Nodejs

35 Views Asked by At

I am working with Nodejs and expressjs,I am using "mysql2 library" and Right now i want to concat and use join (with paramerized) query , How can i do this ? I have following query and i want to write this query in express js (paramerized),How can i do this ?

SELECT `sd`.`service_id`, `sd`.`gender_type_id`, `sd`.`price`, `s`.`service_name`, concat('http://localhost/project/services/', s.image) as image, `s`.`time`
FROM `services_detail` `sd`
JOIN `services` `s` ON `sd`.`service_id`=`s`.`id`
WHERE `sd`.`id` = 13896

1

There are 1 best solutions below

2
Rachel On

If I understand you properly, you want to add a parameter into the query. So just put the whole query inside `` and the parameter inside ${}. for example:

let sample = 5;
let query = `SELECT * from table_name 
WHERE column_name = ${sample}`;