Sql查询范围

时间:2013-05-25 06:07:37

标签: sql sql-server range

我正在开发一个网站 有两种形式的搜索屏幕和serachresultscreen 在第一种形式中,我从两个下拉列表中选择一个范围(即从值1到值2)

范围是分别为顺序的钻石清晰度

FL,IF,VVS,VVS1,VVS2,SI,SI1,I1,I2

当我在下一个表单上查询它时,如

clarity>=FL and clarity<=VVS1

然后它显示字母F到V之间的所有值的结果(即FL,IF,VVS,VVS1,VV2,SI,I1,I2

请建议我回答一下

我需要的输出是(当从= FL到= VVS1之间选择时) 然后仅结果输出值FL,IF,VVS,VVS1

1 个答案:

答案 0 :(得分:1)

您不能将范围运算符BETWEEN用于String值。要解决此问题,请为这些Diamond Clarity值创建具有相应数字权重的表。

基于Clarity的过滤获取其订单然后将所有这些ORDER(数字编号) 在范围运算符

编辑:

CREATE TABLE DIAMOND_CLARITY
(CLARITY VARCHAR(10),
 CLARITY_ORDER int
)

INSERT INTO DIAMOND_CLARITY values ('FL',1)
INSERT INTO DIAMOND_CLARITY values ('IF',2)
INSERT INTO DIAMOND_CLARITY values ('VVS',3)
INSERT INTO DIAMOND_CLARITY values ('VVS1',4)
INSERT INTO DIAMOND_CLARITY values ('VVS2',5)
INSERT INTO DIAMOND_CLARITY values ('SI',6)
INSERT INTO DIAMOND_CLARITY values ('SI1',7)
INSERT INTO DIAMOND_CLARITY values ('I1',8)
INSERT INTO DIAMOND_CLARITY values ('I2',9)

查询以获取范围之间的记录

    SELECT CLARITY from DIAMOND_CLARITY where CLARITY_ORDER 
BETWEEN
    (SELECT CLARITY_ORDER FROM DIAMOND_CLARITY where CLARITY='FL')
    AND 
    (SELECT CLARITY_ORDER FROM DIAMOND_CLARITY where CLARITY='VVS')