I have a working solution, but I was wondering if there was a better way to get the same result.
The scenario:
User interface has a search filter that resolves to a GUID. So if they wanted to search for all records associated with customer John Doe, they would type in John Doe and before the final query is executed John Doe is resolved to John Doe’s customer_id. So if the customer_id is XYZ-456, the query would be:
SELECT id AS item_id FROM items WHERE items.customer_id = 'XYZ-456';
This would return the item ids which would then go through another process to provide the user with the full search results.
But obviously the user might not choose a filter at all. Rather than insert the entire WHERE parameter, I’ve gone with the following solution:
SELECT id AS item_id FROM items WHERE items.customer_id LIKE '%$customer_id%';
So if nothing is given, it resolves to:
SELECT id AS item_id FROM items WHERE items.customer_id LIKE '%%';
Which will return all items (the desired result).
Really, this works fine, but technically their is always the theoretical issue of the following customer_ids XYZ-456, EXYZ-4567, where the shorter of the two ids would return items for both customer_ids.
So is there a “right” way to say “return all results where field = ‘X’ but if ‘X’ is blank, return everything instead of nothing?”
logic like that also works well when filtering on multiple optional criteria.
edit–a similar way