jost jost -5 years ago 108
SQL Question

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

I have table matches with these columns:


  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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download