Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

input if match then filter query

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading