“我的表”包含经验字段,它是一个整数。我的页面包含一个复选框列表,比如0-3,3-7,7-9,9-12,12-15,15+,我必须使用select查询从表中筛选这一点,我尝试过between,但是当选择的多个字段都可以帮助时,它就不能工作了。
我的桌子结构就像
Name Experience in year
---- ---------
a 1
b 2
c 3
d 5
e 2
f 1我的数据库参数是varchar字符串。
if we select 0-3years then '0-3'
if we select 3-6years then '3-6'
if we select both then '0-3,3-6'
if we select 0-3years and 9-12years then '0-3,9-12'现在我用这些格式发送数据,我不知道这是一个好方法,请告诉我更好的方法
发布于 2015-09-10 17:43:59
首先,您需要一个表checkRanges
CREATE TABLE checkRanges
([checkID] int, [name] varchar(8), [low] int, [upper] int);
INSERT INTO checkRanges
([checkID], [name], [low], [upper])
VALUES
(1, '0-3', 0, 2),
(2, '3-6', 3, 5),
(4, '6-9', 6, 8),
(8, '9-12', 9, 11),
(16, '12+', 12, 999)看到checkID是怎样的2的幂吗?
在您的应用程序中,如果用户选择3-6和9-12,则将2+8 = 10发送到数据库。如果您使用db信息创建复选框,也将是很棒的。
在db中,您可以按位比较来选择正确的范围。然后在每一个范围之间穿行。
WITH ranges as (
SELECT *
FROM checkRanges
where checkID & 10 > 0
)
SELECT *
FROM users u
inner join ranges r
on u.Experience between r.low and r.upper一起看吧,SQL Fiddle Demo,我包括了更多的用户。您只需更改clausule where checkID & 10 > 0来测试其他组合。
注:
我更新范围。将上值更改为value - 1,因为between是包含性的,可能会产生重复的结果。
如果要使用旧版本,则必须将联接句中的betwewen替换为
u.Experience >= r.low and u.Experience *<* r.upperhttps://stackoverflow.com/questions/32507413
复制相似问题