php - Cleaner way to write this MySql query -


i generate below query through php

$sql .= "select * test";  $sub_sql = '';  foreach($value $n_k => $n_v) {     switch($n_v)     {         case '1':             $sub_sql .= " or (id = 1 )";          break;         case '2':             $sub_sql .= " or (category = 4) ";         break;         case '3':             $sub_sql .= " or (location = 'u.s.') ";         break;       } }  if($sub_sql != '') {     $sql .= " ( 0 ".$sub_sql." ) "; }  $sql .= "group id  order stored_date desc limit 0, 5 "; 

but you'll can see $subsql concatenated $sql, part looks messy. evn though works fine looks poor. can pls me this?

how adding conditions sql?

something like:

select * test a=1 ,  (   ($n_v = 1 , id = 1)   or   ($n_v = 2 , category = 4)   or   ($n_v = 3 , location = 'u.s.') ) 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -