Reputation: 2545
Here is description of my scenario:
myTable:
ID name width1 width2 parameter1 parameter2 code
1 AAA 0 10 500 750 10001
2 AAA 0 10 750 1250 10002
3 AAA 0 10 900 1400 10003
4 AAA 0 10 1150 1650 10004
.................................................
.................................................
.................................................
Lets say that I'm working with some "subjects" that have their own properties. Based on subject properties and some calculations with that same properties I should take "code" from this table.
So, this one subject that I'm currently working with has name: AAA, width: 5, and with my calculations i get parameter: 1000. After I have all of necessary attributes I can compare them with rows in myTable. For bigger picture I'll select all from this table but my goal is to get code. Here is query for that:
SELECT * FROM myTable WHERE name="AAA" AND 5>=width1 AND 5<=width2 AND 1000>=parameter1 AND 1000<=parameter2
After this I get these two rows:
ID name width1 width2 parameter1 parameter2 code
2 AAA 0 10 750 1250 10002
3 AAA 0 10 900 1400 10003
Just to mention, here could be one row. But, if I get two rows I need to do following. My whole concentration is on these parameters in a way that I'm comparing spread between my parameter and parameter2 in first row and spread between my parameter and parameter1 in second row.
parameter -> parameter2
1250 - 1000 = 250
&
parameter1 -> parameter
1000 - 900 = 100
So, if spread1:250 > spread2:100 take code2:00002 else take code3:00003.
My question is, how to compare values from different rows and columns? As you can see I have two rows and values from different column. What I need is a way to select these specific values for comparison.
Thank you.
Upvotes: 2
Views: 1362
Reputation: 19096
It seems to be solved by Greatest from Least, so at first we get the least value and order this descent.
SELECT
code
FROM
myTable
WHERE
name="AAA"
AND
5 >= width1
AND
5 <= width2
AND
1000 >= parameter1
AND
1000 <= parameter2
ORDER BY
Least( parameter2-1000, 1000-parameter1 ) desc
LIMIT 1;
Upvotes: 2