D
D
Danil2016-01-12 16:47:28
MySQL
Danil, 2016-01-12 16:47:28

How to set optional parameters in mysql?

connection.query('SELECT * FROM `mainreservation` WHERE `status`='+ arh +' AND from_who='+ who +' AND date='+ date +';', function(err, rows, fields) {
if (err) throw err;
})

In general, is it possible to somehow set in WHERE so that if one of the parameters is not specified (well, or specified as "*"), then select all elements? Something like an optional parameter, if there is then a selection on it, if not, then select everything.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Marat, 2016-01-12
@Veneomin

  1. What prevents before the request to check the presence of a parameter through if (or something else) and, depending on the availability, build the necessary request?
    etc. with other types
    But in order to avoid unnecessary loading for the DBMS, it is better to build the right query.

I
IceJOKER, 2016-01-12
@IceJOKER

you need to manually check if there is such a parameter or not, and if it is, then add the query string.

$sql = 'select * from table where 1=1';

if($param1)
  $sql .= " AND param1=param1";

R
Rsa97, 2016-01-12
@Rsa97

WHERE :param = '*' OR `param` = :param

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question