如果我想在select查询的where子句中的某些条件下更改逻辑运算符AND,那该怎么办。
例如
select * from table t where t.column='something' AND t.column='something'
select * from table t where t.column='something' OR t.column='something'
我尝试了什么:
select * from table t where t.column='something'
case some_variable
when some_variable='' THEN OR
ELSE AND
END
t.column='something'
但它会在case子句附近引发语法错误。 请指教。
编辑: 示例小提琴fiddle
答案 0 :(得分:1)
以下已成功运作。
select
DISTINCT
`car_detail`.`car_detail_id` as "Car ID.",
`car_detail`.`status` as "Status"
FROM
`car_detail`
LEFT JOIN `car_reservation_detail` ON car_reservation_detail.car_detail_id=car_detail.car_detail_id
WHERE
(case when car_status='' then (car_detail.status='active' OR car_detail.status='reserved') OR (`car_reservation_detail`.employee_detail_id = emp_id AND (`car_detail`.`status` = 'sold' or `car_detail`.`status` = 'sure'))
else
(`car_reservation_detail`.employee_detail_id = emp_id AND `car_detail`.`status` = car_status AND `car_reservation_detail`.`status`='active')
end)
;