I am trying to generate a query that selects all from a user table where any combination of the last name of first name matches a particular search term
$select = $select->where('last_name LIKE ?', '%'.$term.'%')->orWhere('first_name LIKE ?', '%'.$term.'%')
->orWhere("CONCAT(first_name,' ', last_name) LIKE ?", '%'.$term.'%')
->orWhere("CONCAT(last_name,' ', first_name) LIKE ?", '%'.$term.'%');
There is another condition that has to also has to be met which is specified in another where clause
$select = $select->where("deleted = 0 AND scholar = 0");
The Following SQL statement is generated
SELECT `user`.* FROM `user` WHERE (last_name LIKE '%frank%') OR (first_name LIKE '%frank%') OR (CONCAT(first_name,' ', last_name) LIKE '%frank%') OR (CONCAT(last_name,' ', first_name) LIKE '%frank%') AND (deleted = 0 AND scholar = 0) ORDER BY `date_created` desc LIMIT 25
This doesnt return the desired result as i get rows where scholar = 1;
I figured the query should be
SELECT `user`.* FROM `user` WHERE ((last_name LIKE '%frank%') OR (first_name LIKE '%frank%') OR (CONCAT(first_name,' ', last_name) LIKE '%frank%') OR (CONCAT(last_name,' ', first_name) LIKE '%frank%')) AND (deleted = 0 AND scholar = 0) ORDER BY `date_created` DESC LIMIT 25
What the right syntax to achieve this using the $select object.
I assume deleted and scholar are separate columns. So the easiest way is to just break:
into two statements, like:
this change should result in a sql string like:
also remove the extra
$select =
. Your whole select should likly look something like:You can use quoteInto to prepare your conditions and then use them like this :