Mysql 如何在sql中编写if-else条件
在下面的sql中,如何添加if-else-like控件结构Mysql 如何在sql中编写if-else条件,mysql,sql,Mysql,Sql,在下面的sql中,如何添加if-else-like控件结构 SELECT tblcabbooking.id AS id, concat(tblcabstatus.`status`,' ', ifnull(concat("(INR",tblbookingcharges.totalbill,")"),'')) AS `status`, tblcomplaint.id AS com_id, tblcomplaint.st
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 ;
选择
tblcabbooking.id作为id,
concat(tblcabstatus.‘状态’,’,
如果NULL(concat(((INR),tblbookingcharges.totalbill,“)”)作为“状态”,
TBLCompaint.id作为com_id,
TBLCompaint.statusid作为com_状态,
tblcabbooking.csr_id作为emp,
concat(tblcabbooking.dropaddress,tblcabbooking.droparea)作为drop_区域,
tblbookingcharges.totalbill,
tblcabbooking.booking_参考号,
tblmasterpackage.master\u package作为预订类型,
concat(tblcabbooking.pickupdate,“,tblcabbooking.pickuptime)作为订单时间,
concat(tblclient.clientname,“,tblappid.`type`)作为合作伙伴,
concat(tblcablistmgmt.NAME,,,,tbldriver.firstname,“(”,tbldriver.contactno,”)作为车辆,
concat(tbluserinfo.firstname,“(”,tbluserinfo.mobno,“)”)作为客户名,
concat(tbldriver.firstname,“,tbldriver.contactno)作为司机姓名,
tblcabbooking.mobileno作为mob_no,
tbluserinfo.uid作为客户端id,
tbldriver.uid作为驱动程序id,
concat(tblcabbooking.pickupaddress),tblcabbooking.pickuparea)作为出发点,
tbldriver.VehiclerRegistrationNo AS reg
来自tblcabbooking
加入tblcabstatus
在tblcabbooking上。`status`=tblcabstatus.`status\u id`
加入tbluserinfo
在tbluserinfo.uid=tblcabbooking.clientid上
加入tblmasterpackage
在tblcabbooking.bookingtype=tblmasterpackage.package\u id上
左连接tbldriver
在tblcabbooking.pickup=tbldriver.uid上
左连接TBLCabListGMT
在tbldriver.typeofvehicle=tblcablistmgmt.id上
加入TBLAPID
在tblappid.id=tblcabbooking.partner上
加入tblclient
在tblappid.clientid=tblclient.id上
左连接tblbookingcharges
在tblcabbooking.id=tblbookingcharges.bookingid上
左连接TBL冲突
在tblcabbooking.id=tblcomplaint.bookingid上
其中tblcabbooking.bookingdate>=fromdate
和tblcabbooking.bookingdate否,如果..则不能使用。。ELSE
在普通的ANSI SQL查询中构造,但您可以使用CASE
语句重写它,如
WHERE tblcabbooking.devicetype =
CASE WHEN partnertype LIKE 'Android Booking'
THEN 'ANDROID' ELSE 'Web Booking'
如何使用更改查询的字符串集。左联接时,将这些表的条件从何处移动到ON,以获得真正的外部联接。(现在,这些连接作为常规内部连接执行。)我们在上面插入此代码,因为如果出现其他情况,它将显示错误:-tblcabbooking.devicetype=CASE当partnertype类似于“Android Booking”时,则为“Android”其他情况当partnertype类似于“Web Booking”时,则为“Web”其他情况当partnertype类似于“呼叫中心Booking”时,则为“0”否则tblcabbooking.device按tblcabbooking.id desc键入订单;