Sorry if it's a dumb question but I'm new to express and MySQL and I couldn't find a solution anywhere.
I need to create a MySQL query based on some user input filters from an HTML form. The parameters are something like this:
let sDate = req.body.startDate;
let eDate = req.body.endDate;
let param1 = req.body.param1;
let param2 = req.body.param2;
let param3 = req.body.param3;
let param4 = req.body.param4;
A normal query that I will use if all params are not null will be
const query = `
SELECT * FROM table
WHERE
date BETWEEN ? AND ?
AND col1 = ?
AND col2 = ?
AND col3 = ?
AND col4 = ?;`
db.query(query, [startDate, endDate, param1, param2, param3, param4], (e, rows)=>{});
But every single parameter can be null, so there are a lot of possible filter combinations.
Is there a way to query the db with one single query? I could handle it in many if-else statements but it feels wrong.
Thanks for your help in advance!
paramvalues express the criteria for searching. The interface between the procedural (javascript) realm and the declarative (SQL) realm requires a lot of precision.