I dont want to write two statements like this
if(filter_by_company) {
query..
} else {
query...
}
So select users and they are in companies and I the admin can filter like by company id. How can I say when filter not exists then dont make the condition ?
SELECT * FROM users u
LEFT JOIN clients c
ON c.id = u.company_id
WHERE c.company_id = 'NAME' IF EXISTS ?? ELSE DONT ASK
how can I do it ?
3
Answers
try this
Assuming you are passing
company_id
as a positional parameter, pass it twice in succession (passnull
in your programming language when you don’tfilter_by_company
):If your client allows named parameters, you can use:
or some other equivalent parameter syntax.
What I think you’re asking is how do you write one query which can handle both filtering by company, and not filtering by company.
and
There’s two approaches. First is to try to construct this as one query. The second is to use a more flexible approach to building queries.
To do it in one query, you’d pass in a boolean flag to determine whether or not to filter by company.
Demonstration.
This is acceptable for something simple, but this technique is very limited and leads to unnecessarily complex queries.
Far better is to use a SQL query builder to build up your queries. Then you can write a generic query and add clauses onto it. What you use depends on your choice of programming languages, for these examples we’ll use Knex.js.
First, build the basic query.
And add to it as needed.
filter_user_by_client
is a new object,user_client_query
is unaffected.The beauty of this technique is you can add limits, orders, more where clauses, more joins, and so on.
And it will adapt to SQL dialects for you.