Is this a simplified example? Because I see no reason not to simplify it like this:
$sql .= "Select * from test where a=1";
foreach($value as $n_k => $n_v)
{
switch($n_v)
{
case '1':
$sql .= " AND (id = 1 )";
break;
case '2':
$sql .= " AND (category = 4)";
break;
case '3':
$sql .= " AND (location = 'U.S.')";
break;
}
}
$sql .= " ORDER BY stored_date DESC LIMIT 0, 5 ";
Note, that I removed the GROUP BY
from your statement. You shouldn't use it, if you don't use aggregate functions. Group by collapses group of rows and displays random rows from each group. If you have a need to remove duplicate rows, consider to use the DISTINCT
keyword in the SELECT
clause.
UPDATE:
Your new version can be shortened like this:
$sql .= "Select * from test";
foreach($value as $n_k => $n_v)
{
switch($n_v)
{
case '1':
$sql .= " where (id = 1 ) ";
break;
case '2':
$sql .= " where (category = 4) ";
break;
case '3':
$sql .= " where (location = 'U.S.') ";
break;
}
}
$sql .= " GROUP BY id ORDER BY stored_date DESC LIMIT 0, 5 ";
Basically it comes down to this. If you have to add multiple AND
conditions, you always add a WHERE 1=1
(if you have no other fix (non-dynamic) conditions) to the query. It doesn't change anything, the optimizer will throw it away again. But you can easily append as many conditions as you want. That's what I did in my original answer before your edit. Note though, that you can not just append OR whatever = true
when you are mixing AND
and OR
conditions. You will have to use parantheses to not get a false resultset. This is where your (0 OR ...)
thing comes from. Since you only have 1 condition, there's no need at all for all I said right now. Just add it as WHERE whatever = true
and that's it.