【问题标题】:using if else condition with null value after where在 where 之后使用带有空值的 if else 条件
【发布时间】:2015-09-14 04:11:30
【问题描述】:
select  tbc.ID,tbc.Booking_reference,
    concat(tbc.PickupDate , tbc.pickuptime) as PickupTime ,
    (select master_package 
         from Tblmasterpackage 
         where package_id =tbc.BookingType) as Booking,
    concat('Hello42 ', tbc.DeviceType) as Partner,concat( tbc.UserName , tbc.MobileNo ) as ClientName,
    tbc.DropArea as droparea,
    tbc.PickupArea as departure,
    tbc.`Status`
    from tblcabbooking tbc 
    inner join  tbldriver td
    on tbc.pickup=td.uid
    inner join Tblmasterpackage tmp
    on tmp.package_id=tbc.BookingType

where  tbc.Bookingdate >= FromDate  AND tbc.Bookingdate <= Todate

when IF CallerID is not null
    THEN
and tbc.MobileNo=CallerID
END IF

when IF book_ref is not null
THEN
    and tbc.Booking_reference=book_ref
END IF

when IF  vehicle_number is not null
THEN
    and td.VehicleRegistrationNo=vehicle_number
END IF

when IF vehicle_Driver is not null
THEN
    and td.FirstName=vehicle_Driver
END IF

when IF PartnerType='Android Booking'
then
  and tbc.DeviceType='ANDROID'
END IF

when IF PartnerType='Web Booking'
THEN
    and tbc.DeviceType='WEB'
END IF

when IF  PartnerType='Call Center Booking'
THEN
    and tbc.DeviceType='0'
END IF

when IF  PartnerType='All Booking'
THEN
END IF

order by ID desc limit limit_check,limite_off;

【问题讨论】:

  • 你忘了问问题
  • 请说明在 where case 中如何使用 null 值
  • 我们使用七个值字段进行查询,因此在某些情况下某些值将为空,因此如何处理在 mysql 过程中使用空值的情况

标签: mysql database conditional


【解决方案1】:

你可以试试这个

        and (tbc.Mobileno=IFNULL(CallerID,tbc.MobileNo) or (CallerID = null tbc.Mobileno =null))

    and ( tbc.Booking_reference=IFNULL(book_ref, tbc.Booking_reference) or (book_ref=null  tbc.Booking_reference = null))

    and ( td.VehicleRegistrationNo=IFNULL(vehicle_number, td.VehicleRegistrationNo) or ( vehicle_number =null  td.VehicleRegistrationNo = null))

and ( td.FirstName=IFNULL(vehicle_Driver, td.FirstName) or ( vehicle_Driver = null  td.FirstName = null))

and ( (PartnerType='Android Booking' and tbc.DeviceType='ANDROID')  

or ( PartnerType='Web Booking' and tbc.DeviceType='WEB')

or ( PartnerType='Call Center Booking' and tbc.DeviceType='0')

or ( PartnerType='All Booking' and 1=1 )

)

【讨论】:

  • 我需要如果 Mobileno 的值为空,则不考虑这种情况,并且所有过程变量都相同
  • or (CallerID is null tbc.Mobileno is null) 这个条件覆盖空记录
  • 所以如果我们考虑这一点,那么如果我们将所有相关的所有数据都发送为空,您确定它会从受尊重的表中获取所有数据
  • 试试你。如果有任何问题,请告诉我
  • 创建过程时出现错误 SQL 错误 (1064):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以了解在 'tbc.Mobileno is null)) 和(第 16 行的 tbc.Booking_reference=IFNULL(book_ref, tbc.Booki' )附近使用的正确语法
猜你喜欢
  • 1970-01-01
  • 2013-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-09
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
相关资源
最近更新 更多