dsxi70423 2019-02-19 06:25
浏览 108
已采纳

如果mysql中的if子句选择

My full query is:

$sql = "SELECT vehicle_id,  fuel_type, gearbox_type, door_type, engine_package_name, FORMAT(engine_cap, 1)engine_cap, engine_power, FORMAT(fuel_cons, 1)fuel_cons, FORMAT(std_price, 0)std_price 
FROM vehicle 
WHERE model_id = '$model_id' 
AND fuel_type IN ('$fuel_type1', '$fuel_type2', '$fuel_type3', '$fuel_type4') AND gearbox_type IN( '$gearbox_type1', '$gearbox_type2', '$gearbox_type3', '$gearbox_type4') AND door_type IN ('$door_type1', '$door_type2', '$door_type3', '$door_type4')" ;

I would like to skip the part of the query shown below if $fuel_type1 equals to zero.

AND fuel_type IN ('$fuel_type1', '$fuel_type2', '$fuel_type3', '$fuel_type4') 

I have tried

$sql = "SELECT vehicle_id,  fuel_type, gearbox_type, door_type, engine_package_name, FORMAT(engine_cap, 1)engine_cap, engine_power, FORMAT(fuel_cons, 1)fuel_cons, FORMAT(std_price, 0)std_price 
FROM vehicle 
WHERE model_id = '$model_id' ";

if($fuel_type1 != 0) 
{ 
    $sql += “ AND fuel_type IN ('$fuel_type1', '$fuel_type2', '$fuel_type3', '$fuel_type4')”;
}

didn't worked.

Also I have tried

$sql = "SELECT vehicle_id, fuel_type, gearbox_type, door_type, engine_package_name, FORMAT(engine_cap, 1)engine_cap, engine_power, FORMAT(fuel_cons, 1)fuel_cons, FORMAT(std_price, 0)std_price 
FROM vehicle 
WHERE model_id='$model_id’ 
AND IF($fuel_type1 <> 0,'fuel_type','0') IN ('$fuel_type1', '$fuel_type2', '$fuel_type3', '$fuel_type4’)";

didn't worked :((

any help will be appreciated.

  • 写回答

1条回答 默认 最新

  • duanji9378 2019-02-19 07:16
    关注

    problem is in your concatenation process

    in PHP there should b a do t. to concatenate not + sign

    if($fuel_type1 != 0) 
    { 
        $sql .= “ AND fuel_type IN ('$fuel_type1', '$fuel_type2', '$fuel_type3', '$fuel_type4')”;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 微信聊天记录备份到电脑提示成功了,但还是没同步到电脑微信
  • ¥15 python怎么在已有视频文件后添加新帧
  • ¥20 虚幻UE引擎如何让多个同一个蓝图的NPC执行一样的动画,
  • ¥15 fluent里模拟降膜反应的UDF编写
  • ¥15 MYSQL 多表拼接link
  • ¥15 关于某款2.13寸墨水屏的问题
  • ¥15 obsidian的中文层级自动编号
  • ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!