谁能帮我找出为什么它没有给出正确的值

问题描述 投票:0回答:1

当我给出所有值时,它给出正确的值。但是当我只给车辆名称时,它会给出磨损值。请帮助我为什么它不起作用

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}

我想获取正确的值。在任何条件下

sql mysql mysqli oracle-sqldeveloper mysql-workbench
1个回答
0
投票

您可以尝试使用 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}```
© www.soinside.com 2019 - 2024. All rights reserved.