I have a table that stores part numbers for various items like so:
+------+-------------+-------------+-------------+
| Item | Color | Type | Part Number |
+------+-------------+-------------+-------------+
| Pen | | | 001 |
| Pen | red | felt | 002 |
| Pen | blue | felt | 003 |
| Pen | | felt | 004 |
| Pen | green | felt | 005 |
| Pen | | gel | 006 |
| Pen | green | gel | 007 |
+------+-------------+-------------+-------------+
I need to get the part numbers for items that were ordered in the past. Is there a way to match the "correct" row with a single query knowing the Item, Color and Type?
Given {Item = Pen, Color = orange, Type = felt} the result should be 004
Given {Item = Pen, Color = blue, Type = felt} the result should be 003
Given {Item = Pen, Color = blue, Type = gel} the result should be 006
I'd like to avoid populating every possible combination into the table if possible.
item = pen,color = blueandtype = gelgets you006, is there a typo or what is the logic that you are using? Also where is thecolor = orangeto get a result of004? Do you want the row that returns the most items matched?item = pen, color = blue, type = NULL, part_number=008. What should the queryitem = pen, color = blue and type = gelreturn then?006or008? Both would have two matches.ORDER BY (pen='blue')+(type='gel')+(color='blue') DESC, (pen='blue')+(type='gel') DESC, (pen='blue') DESC LIMIT 1;