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

1 回答 1

1

就像评论中建议的那样,您应该重新设计您的数据库:

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)
)

然后您可以进行如下查询:

SELECT DISTINCT StyleID FROM ZipCodes WHERE RangeBegin >= 85012 AND RangeEnd <= 85012
于 2013-08-05T18:09:11.287 回答