I heve the following two tables in MySQL.
Items
| code | value |
|---|---|
| 1 | 6 |
| 2 | 8 |
Locations
| code | min | max | location |
|---|---|---|---|
| 1 | 5 | 8 | loc1 |
| 1 | 4 | 9 | loc2 |
| 2 | 6 | 10 | loc3 |
I want to get the location for each code in Items, where there is the biggest difference between min and max. For code=1, there are two locations assigned, loc1 and loc2, but the correct one is loc2 because 9-4 is bigger than 8-5.
The output would be
| code | value | location |
|---|---|---|
| 1 | 6 | loc2 |
| 2 | 8 | loc3 |