jost jost - 5 months ago 18
SQL Question

SQL - If input string is empty return all rows , otherwise try to match

I have table matches with these columns:

|sport|region|country|league



  1. If the input string for sport is empty I want to return everything and don't bother with matching region, country etc.

  2. If sport is not empty then find rows with matched sports and proceed to region and do the same thing.



Is this possible to do in SQL? I know I can filter this out in PHP and then run different SQL queries.

Answer

Try this

WHERE (sport_param IS NULL OR sport_column = sport_param)

You might want to use the LIKE operator or consider case-insensitive checking instead of simply comparing the exact sport.

Comments