1

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

4
  • There's nothing different about using parameters in a join. Just replace the values that you want to parametrize with ?. Commented May 11, 2023 at 17:16
  • E.g. WHERE sd.id = ? Commented May 11, 2023 at 17:17
  • @Barmar Kindly update/post the answer so i can check Commented May 11, 2023 at 17:19
  • 1
    Please post your attempt so I can help you fix the problem. Commented May 11, 2023 at 17:25

1 Answer 1

1

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}`;

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

2 Comments

@diana If your question was about how to connect to the db by mysql2 or about another topic, let me know and I will Edit the post.
Yeah, that's it! Beware sql injections, but mysql and mysql2 have ways to deal with this issue, if that's the question being asked. I could not figure it out exactly too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.