I’m having some trouble with retrieving some records from my database. Many of the pages that are on my site are tagged with various values to aid in searching/organization. One of these values was set up with a checkbox. If the box is checked in the backend the value is “1”. If it has not been checked it’s blank.
Here is my SELECT statement. It is used in an AJAX search for site visitors to search for products.
SELECT *,
MAX( CASE WHEN name = "is_active" THEN value END ) AS is_active,
GROUP_CONCAT(...
...
HAVING is_active LIKE "%'.$active.'%"
I’ve omitted a portion of my SELECT statement to conserve space.
The statement works perfectly except for the example I gave. If the visitor doesn’t check the active checkbox on the front end it returns results as if it was checked…or equal to “1”. There is no change if the box is checked. It seems like I can’t get it to return the records where is_active is equal to “”. What am I doing wrong?
It sounds as though the field in your database has the default value as
NULL. You can add a second condition in yourHAVINGclause to check for this:EDIT
I just saw you define
is_activein the select list. WithMAX(), the empty values will be ignored. You can use a nested-CASEto fix this: