Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

how do i create an if clause within a mySQL query sub

$
0
0
Hi everyone.

i am trying to create an "if clause" within a sub query of a mySQL QUERY. i have enclosed the query below .


This is the part of the query that i want to place in an "If Clause"

PHP Code:


   WHERE   user_id IS NOT NULL
                       
if(!empty($Co_lo) )
                {
                     AND  
job_id IN 1,2,3,4,5,6,7,8,9,10  )
                    } 




The Search Query.

PHP Code:



function worker_general_search (   $Co_lonull$last_login=null ,  $limit=null    )

    
     {             
             global 
$dbc;
                      
    
    
$select "      SELECT 
                 u.user_id, 
                  u.first_name, 
                  cl.country_id,
                  col.country,
                  jb.job_category_sort    
               
                " 

    
                  
    
$from   "     FROM 
                          users u  
                 LEFT OUTER  JOIN   countrylocation cl
                    ON u.user_id = cl.user_id
                 LEFT OUTER  JOIN  countrylist col  
                    ON cl.country_id = col.country_id   
                 
                    
            LEFT OUTER  JOIN 
                (  SELECT user_id,
                    GROUP_CONCAT(job_id) AS job_category_sort
                   FROM worker_category 
                
                    WHERE   user_id IS NOT NULL
                       if(!empty(
$Co_lo) )
                {
                     AND  job_id IN ( 1,2,3,4,5,6,7,8,9,10  )
                    }
                      
                      GROUP
                     BY user_id
                                             
                         ) AS jb
                ON jb.user_id = u.user_id

                
                
                    "
;
          
                  
$where      "     WHERE   u.live  = '1'" ;
        
            
$order=  "     ORDER BY   u.first_name DESC $limit ";  
         
           
$query $select.$from.$where$order ;
     
//   $query = $select.$from.$where. $GROUPBY. $order ;
         
    
        
        
$result = @mysqli_query ($dbc$query)or trigger_error("Query: $query\n<br />MySQL Error: " mysqli_error($dbc))    ;
        
          
         return 
$result 
         
         
    }      
    
the function is a search boxso i need the if clause beucase i only want the "AND" condition to apply if  the user specifies itthat is why it needs to be contained within an "IF CLAUSE"

Thank you so much for your kind attention 


Viewing all articles
Browse latest Browse all 13329

Trending Articles