在下面的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 ;
如何使用設置字符串的那些更改查詢。 –
當LEFT JOIN時,將這些表的條件從WHERE移動到ON,以獲得真正的外連接。 (因爲它是現在,這些連接執行定期的內部連接。) – jarlh