I have a need to select a filename based on certain options. I have encoded a series of options in a database table, as such:
__A___B___C__ .. __FEET_____FILENAME_____
| * | * | * | | 0 | ABC.pdf |
------------------------------------------|
| * | | * | | 1 | A_C_1foot.pdf |
------------------------------------------
Where several fields, such as A, B, C are booleans, and some other fields, like FEET is a positive integer. When FEET is 0, it means that it is a wildcard, and any positive integer should match that row. I have several other variables like FEET. Those are SHELLS, MODEL.
Only one row max should be returned for any set of options, and only one row min should be returned, since the table guarantees there is one match, but does not guarantee that it is exact. In other words always return exact match (or best match if exact is not avaialble), and one or the other ro both are guaranteed to be there.
Problem
I don't know ahead of time if the table has exact matching row, or if it has wildcards. It is possible that there may even be both, where exact result should be favored, if available. My SQL is like this:
SELECT filename
FROM table
WHERE A = $a
and B = $b
and C = $c
and FEET = $feet
and MODEL = $model
and SHELLS = $shells;
When there is no exact match for all FEET, MODEL, and SHELLS, it fails and returns 0 rows for certain FEET, SHELLS, MODEL values. I need to return best matched row.