当我给出所有值时,它给出正确的值。但是当我只给车辆名称时,它会给出磨损值。请帮助我为什么它不起作用
SELECT * FROM vehicles v INNER JOIN vehicles_check_in vci ON v.vehiclesid=vci.vehicleid 内部连接 vehicles_check_out vco ON vci.vehicle_checkin = vco.vehicle_checkin_id WHERE vehiclename LIKE '%$vechicleNameFilter%' AND vehiclercnumber LIKE '%$vechicleRCNoFilter%' AND vehicle_gross_weight LIKE '%$vechicleCheckInWeightFilter%' AND vehicle_checkout_gross_weight LIKE '%$vechicleCheckOutWeightFilter%' AND vehicle_gross_weight_type LIKE '%$vehicleWeightTypeFilter%' AND vehicle_checkin_time BETWEEN '$vehicleFromDateFilter' AND '$vehicleToDateFilter' ORDER BY vehicle_checkin DESC LIMIT {$offset}, {$limit_per_page}
我想获取正确的值。在任何条件下
您可以尝试使用 IFNULL 来处理未设置变量的情况。
例如:
SELECT * FROM vehicles v
INNER JOIN vehicles_check_in vci ON v.vehiclesid=vci.vehicleid
INNER JOIN vehicles_check_out vco ON vci.vehicle_checkin = vco.vehicle_checkin_id
WHERE vehiclename LIKE IFNULL('%$vechicleNameFilter%', '%')
AND vehiclercnumber LIKE IFNULL('%$vechicleRCNoFilter%', '%')
AND vehicle_gross_weight LIKE IFNULL('%$vechicleCheckInWeightFilter%', '%')
AND vehicle_checkout_gross_weight LIKE IFNULL('%$vechicleCheckOutWeightFilter%', '%')
AND vehicle_gross_weight_type LIKE IFNULL('%$vehicleWeightTypeFilter%', '%')
AND vehicle_checkin_time BETWEEN '$vehicleFromDateFilter' AND '$vehicleToDateFilter'
ORDER BY vehicle_checkin DESC LIMIT {$offset}, {$limit_per_page}```