1

i have a column in database that stores a range of user ages, like 5-10, 10-18, 18-25....

Now i want to write a query to fetch the columns with age matching this range.

For example, if i pass the age as 12, it should fetch the columns that fit in the range 10-18. If i pass the age as 10, it should fetch the columns that fit in the range 5-10.

But the fact is that, i am storing the range as a varchar type (there is a hyphen in between).

So i am curious about how to achieve this, and how to construct a query.

3
  • 1
    Do you have to stick to your database design? Otherwise I would change that - will make lif a lot easer for you Commented Mar 11, 2011 at 11:34
  • Don't store the age range in this way. Commented Mar 11, 2011 at 11:37
  • Sorry to say that I cant change the database design right now... Commented Mar 11, 2011 at 11:53

4 Answers 4

3

You could split up the values using SUBSTRING_INDEX

SELECT * FROM `yourtable` WHERE 5 BETWEEN SUBSTRING_INDEX(`agespan`,'-', 1) AND SUBSTRING_INDEX(`agespan`,'-', -1) 

This query asumes agespan is the column containing 5-10, 12-18.

SUBSTRING_INDEX(agespan,'-', 1) matches everything in the string before the first occurence of -

SUBSTRING_INDEX(agespan,'-', -1) matches everything in the string after the last occurence of -

Sign up to request clarification or add additional context in comments.

Comments

1

I think that is going to be asy (if you can) to change a bit the data model and add to numeric fields for the range for example:

| MIN_AGE | MAX_AGE |
    5           10
    11          18

so you can do a select between the two values.

Aswell i recomend you to not use the same value for two blocks, for example, if you have 5-10 the next should be 11-18.

If you cant change the data model i will probably get the data and process it in the PHP or make some king of stored procedure in the database to process de thing, but this is a non trivial task.

EDIT 1:

If you have not too many ranges you can get all of them to your PHP code and afer convert the "5-10" string in two integers using this expresion:

"""
^           # Assert position at the beginning of a line (at beginning of the string or after a line break character)
(           # Match the regular expression below and capture its match into backreference number 1
   [0-9]       # Match a single character in the range between “0” and “9”
  +           # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
-           # Match the character “-” literally
(           # Match the regular expression below and capture its match into     backreference number 2
   [0-9]       # Match a single character in the range between “0” and “9”
   +           # Between one and unlimited times, as many times as possible, giving    back as needed (greedy)
)
$           # Assert position at the end of a line (at the end of the string or before a line break character)
"""
^([0-9]+)-([0-9]+)$

This way you ar goint to get two capture groups, group 1 with the min and group 2 with the max value. At this point is easy to check if the value you have is in the range.

I hope this make sense for you, please let me know if not :)

4 Comments

This would have been the best solution, but as i said, i cant change the db design now.. so is there any alternative like regexp...
How many registers (ranges) are you going to have? is it a fixed number?
the ranges vary from 0 to 100. Some examples are 0-5, 6-10, 20-30, 20-40, 50-100, 90-100, 0-100
It was a nice solution, but i was looking for a mysql hack instead. Thanks for the solution though.
1

You'll need to split the string in two parts, the part before the "-" and the part after the "-".

SELECT range INTO @Range FROM ATable WHERE ...
/* or for a quick test: SET @range = "5-10"; */
SET @dash_pos = LOCATE("-",@Range);
SET @from = LEFT(@Range,@dash_pos-1);
SET @to = RIGHT(@Range, LENGTH(@Range) - (@dash_pos));

/*debug code to test if @from and @to are filled correctly*/
SELECT @Range, @dash_pos, @from, @to;
/*remove in production*/    

SELECT * FROM tableName 
  WHERE tablename.age BETWEEN @from AND @to;

Comments

0

I don't get your question correctly but you should try this code out...

$query = "SELECT * FROM tablename BETWEEN $int_1 AND $int_2"

But for that varchar thingy I don't know...

Sorry..

But I hope this would work!

1 Comment

This is not working for the OP case, the field he is using is a string in the way "5-10" so he can't use the between operator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.