2015-07-02 49 views
-4

在下面的sql中,如何添加if-else像控制結構。如何在sql中編寫if else條件

SELECT 
     tblcabbooking.id AS id, 
     concat(tblcabstatus.`status`,' ', 
     ifnull(concat("(INR",tblbookingcharges.totalbill,")"),'')) AS `status`, 
     tblcomplaint.id AS com_id, 
     tblcomplaint.statusid AS com_status, 
     tblcabbooking.csr_id AS emp, 
     concat(tblcabbooking.dropaddress,tblcabbooking.droparea) AS drop_area, 
     tblbookingcharges.totalbill, 
     tblcabbooking.booking_reference AS ref, 
     tblmasterpackage.master_package AS booking_type, 
     concat(tblcabbooking.pickupdate," ",tblcabbooking.pickuptime) AS ordertime, 
     concat(tblclient.clientname," ",tblappid.`type`) AS partner, 
     concat(tblcablistmgmt.NAME,', ',tbldriver.firstname,"(",tbldriver.contactno,")") AS vehicle, 
     concat(tbluserinfo.firstname,"(",tbluserinfo.mobno,")") AS clientname, 
     concat(tbldriver.firstname," ",tbldriver.contactno) AS driver_name, 
     tblcabbooking.mobileno AS mob_no, 
     tbluserinfo.uid AS client_id, 
     tbldriver.uid AS driver_id, 
     concat(tblcabbooking.pickupaddress,' ',tblcabbooking.pickuparea) AS departure, 
     tbldriver.vehicleregistrationno AS reg 
    FROM  tblcabbooking 
    JOIN  tblcabstatus 
    ON  tblcabbooking.`status`=tblcabstatus.`status_id` 
    JOIN  tbluserinfo 
    ON  tbluserinfo.uid=tblcabbooking.clientid 
    JOIN  tblmasterpackage 
    ON  tblcabbooking.bookingtype=tblmasterpackage.package_id 
    LEFT JOIN tbldriver 
    ON  tblcabbooking.pickup=tbldriver.uid 
    LEFT JOIN tblcablistmgmt 
    ON  tbldriver.typeofvehicle=tblcablistmgmt.id 
    JOIN  tblappid 
    ON  tblappid.id=tblcabbooking.partner 
    JOIN  tblclient 
    ON  tblappid.clientid=tblclient.id 
    LEFT JOIN tblbookingcharges 
    ON  tblcabbooking.id=tblbookingcharges.bookingid 
    LEFT JOIN tblcomplaint 
    ON  tblcabbooking.id=tblcomplaint.bookingid 
    WHERE  tblcabbooking.bookingdate >= fromdate 
    AND  tblcabbooking.bookingdate <= todate 
    AND  tblcabbooking.mobileno=ifnull(callerid,tblcabbooking.mobileno) 
    AND  tblcabbooking.booking_reference=ifnull(book_ref, tblcabbooking.booking_reference) 
    AND  tbldriver.vehicleregistrationno=ifnull(vehicle_number, tbldriver.vehicleregistrationno) 
    AND  tbldriver.firstname=ifnull(vehicle_driver, tbldriver.firstname)IF partnertype LIKE 'Android Booking' then 
    AND 
    tblcabbooking.devicetype='ANDROID' 
    ELSE 
    IF partnertype LIKE 'Web Booking' then 
    AND 
    tblcabbooking.devicetype='WEB' 
    ELSE 
    IF partnertype LIKE 'Call Center Booking' then 
    AND 
    tblcabbooking.devicetype='0' 
    ELSE 
    and 
    partnertype=tblcabbooking.devicetype 
    END 
    IF ORDER BY tblcabbooking.id DESC ; 
+0

如何使用設置字符串的那些更改查詢。 –

+0

當LEFT JOIN時,將這些表的條件從WHERE移動到ON,以獲得真正的外連接。 (因爲它是現在,這些連接執行定期的內部連接。) – jarlh

回答

2

NO,你不能在正常的ANSI SQL查詢中使用IF .. ELSE結構,但可以使用CASE聲明它改寫像

WHERE tblcabbooking.devicetype = 
       CASE WHEN partnertype LIKE 'Android Booking' 
       THEN 'ANDROID' ELSE 'Web Booking' 
+0

我們插入這個代碼在上面,如果其他條件,它會顯示錯誤: - tblcabbooking.devicetype = \t \t \t \t \t CASE WHEN partnertype LIKE「的Android預定」,然後 'ANDROID' 其他CASE WHEN partnertype LIKE '網絡預約' THEN 'WEB' \t \t \t \t \t其他CASE WHEN partnertype LIKE '呼叫中心預定',然後 '0' \t \t \t \t \t其他THEN tblcabbooking。 devicetype order by tblcabbooking.id desc; –