I want to pass a input to sql and if the input i passed is reg_number is '22162' then I filter with the mdate = '2023-10-20' else I just filter with reg_number is '13352'
How do i check in where statement whether the input is passed is ‘22162’ and then filter on that? Please help
Code I tried but with error:
SELECT * FROM car_details
WHERE (reg_number = ? AND reg_number = '22162' AND mdate = '2023-10-20')
OR reg_number = '13352'
>Solution :
you can try this query:
SELECT * FROM car_details
WHERE
(CASE WHEN ? = '22162' THEN reg_number = '22162' AND mdate = '2023-10-20' ELSE reg_number = '13352' END)
I simply use the CASE WHEN statement. You can also use this to modify my query as you want.
EDIT:
If you want to use AND/OR you can do something like this:
SELECT * FROM car_details
WHERE
(reg_number = ? AND reg_number = '22162' AND mdate = '2023-10-20')
OR
(reg_number = '13352' AND reg_number <> '22162')