www.webdeveloper.com
Results 1 to 3 of 3

Thread: if statement on sql query

  1. #1
    Join Date
    May 2012
    Posts
    17

    if statement on sql query

    I was recomended to use the code as bellow to check if one of the condition not selected for example what if somebody will select just Mark=toyota and will leave empty Model and Year.

    PHP Code:
    $where = array();
    $whereclause '';

    if (!empty(
    $mark)) {
        
    $where[] = "a.mark = '$mark'";
    }
    if (!empty(
    $model)) {
        
    $where[] = "a.model = '$model'";
    }
    if (!empty(
    $year)) {
        
    $where[] = "a.year = $year";
    }
    if (
    count($where 0)) {
        
    $whereclause join(' AND '$where);


    PHP Code:
    $result mysql_query("SELECT * FROM `add_new_car` AS a INNER JOIN `image_add_car` AS b ON
                                     a.id_add_car = b.user_id WHERE (b.user_id, b.id_image_add_car) IN (SELECT user_id, MIN(id_image_add_car)
            
    FROM image_add_car GROUP BY user_id) AND "
    .$whereclause." ORDER BY a.id_add_car"); 

    Mark=toyota Model=Prado Year=2013

    But why when I chose just Mark it doesn't gives me any result but at least together with Model=Prado it works without selecting Year

    Thank you

  2. #2
    Join Date
    May 2012
    Posts
    17
    Thank you Ive just misplased model and mark

  3. #3
    Join Date
    May 2013
    Location
    Surendranagar Gujarat India
    Posts
    55
    SELECT f.*, IF(f.buyable, s.cost, '') as cost
    FROM file as f
    IF f.buyable THEN
    LEFT JOIN subscription as s ON s.subscriptionid = f.subscriptionid
    END IF



    Kishor Makwana
    Software Engineer
    Insight Softech
    www.insightsoftech.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles