0

Having a bit of an issue with this one. Is there a way to check if a value is in the range provided in a mysql query result.

How can I see if the Zip Code 85012 is within the ZipCodes field returned without first making a query, breaking it up into parts, and using BETWEEN?

+---------+-------------+--------------------+------------+-------------+
| StyleID | HistStyleID | AutobuilderStyleID | FilterRule | ZipCodes    |
+---------+-------------+--------------------+------------+-------------+
|  355755 |  2013490103 | w2013k49m1t3       | includes   | 27000-36999 |
+---------+-------------+--------------------+------------+-------------+

:: UPDATE ::

This table can also contain various ranges as follows..

+---------+-------------+--------------------+------------+-------------------------------------+
| StyleID | HistStyleID | AutobuilderStyleID | FilterRule | ZipCodes                            |
+---------+-------------+--------------------+------------+-------------------------------------+
|  332492 |  2012493107 | w2012k49m31t7      | excludes   | 38600-39799,70000-71499,71600-79999 |
+---------+-------------+--------------------+------------+-------------------------------------+
4
  • Does you Zipcodes field holds just 2 zipcodes or more than that? Any ways it looks shabby. Commented Aug 5, 2013 at 17:52
  • Looking at the table it can be various ranges. 38600-39799,70000-71499,71600-79999 Commented Aug 5, 2013 at 17:54
  • 4
    This sounds like a table normalization problem. I would probably add a table that relates to this table and has ZipCodeRangeStart and ZipCodeRangeEnd fields. You could then JOIN that table to this based on the value you are interested being BETWEEN those values. Commented Aug 5, 2013 at 17:58
  • This database is from Chrome Data so redesign is unfortunately not an option. Commented Dec 16, 2014 at 3:43

1 Answer 1

1

Like it's been suggested in the comments, you should redesign your database:

CREATE TABLE Styles (
     StyleID INT PRIMARY KEY,
     HistStyleID INT, 
     AutobuilderStyleID VARCHAR(40),
     FilterRule VARCHAR(40)
)


CREATE TABLE ZipCodes (
     StyleID INT,
     RangeBegin INT,
     RangeEnd INT,

     FOREIGN KEY StyleID REFERENCES Styles(StyleID)
)

Then you can make queries like:

SELECT DISTINCT StyleID FROM ZipCodes WHERE RangeBegin >= 85012 AND RangeEnd <= 85012
Sign up to request clarification or add additional context in comments.

Comments

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.