Getting value between two values in DB

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Getting value between two values in DB

Post by GeXus »

I have a table with from_value and to_value... I want to return the results where value X is between from_value and to_value...

so I will have a value such as 25... then I want to select which record it is associated to based on the from_value and to_value values in the DB...


Any idea how I can do that? It's probably dead simple.. but I'm not getting it... thanks!
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Can you elaborate a little? I don't fully understand what your trying to do.

Maybe post the table structure.
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

Sure, so I have a table like this

id
name
from_value
to_value


Then I have a quiz with 5 questions and 5 options per question... each option is worth a certain value... The values of each is totaled and I want to return the row from the above table where that value is in between the from_value and to_value
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

GeXus wrote:Sure, so I have a table like this

id
name
from_value
to_value

Then I have a quiz with 5 questions and 5 options per question... each option is worth a certain value... The values of each is totaled and I want to return the row from the above table where that value is in between the from_value and to_value

Code: Select all

SELECT * FROM [tablename] WHERE $myvalue >= `from_value` AND $myvalue <= `to_value`
should do it.
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

Haha, nice.. thank you!!
Post Reply