如何在sql中编写if else条件

时间:2015-07-02 05:43:33

标签: mysql sql

在下面的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 ;

1 个答案:

答案 0 :(得分:2)

不,你不能在普通的ANSI SQL查询中使用IF .. ELSE构造,但你可以使用CASE语句重写它,如

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