2

i am passing parameters in a query in the WHERE clause. I would like to create a case where any param in the where is fine (like obmitting the value).

for example:

SELECT * FROM USERS WHERE lang=%s

i can pass in the placeholder strings like 'en', 'it', 'es'. but what about if i want to take everything in the lang column? like if i didn't mention the lang in the where clause?

is there something like:

SELECT * FROM USERS WHERE lang=ANYTHING

??

1
  • 1
    Something similar to: WHERE lang=%s OR %s IS NULL Commented Aug 21, 2018 at 12:58

1 Answer 1

2

You can use NULL as an indicator you don't care about the language. Then you can use IS NULL to check if the value is null and therefore include all rows. Use OR to append the equality check. Like that, if the value isn't null, it must match the language for a row to be returned.

SELECT *
       FROM users
       WHERE %s IS NULL
              OR lang = %s;
Sign up to request clarification or add additional context in comments.

2 Comments

thank you. and i pass always the same value to both those palceholders right?
@91DarioDev: Yes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.