Sql query Where clause filter on code number 如何在没有过滤器的情况下获取所有记录?
Sql query Where clause filter on code number how to get all record without filter?
我想按照我的查询所示明智地获取数据状态,
select Zipcode,
(CASE pobox
WHEN 1 THEN 'Available'
WHEN 2 THEN 'Reserved'
WHEN 3 THEN 'Assigned'
WHEN 4 THEN 'Closed'
END) as ' Status'
from custom_pobox
where pobox=@Status
假设@status = 0 或 1 或 2 任意值
但是我们在数据库中有从 0 到 4 的值我想显示所有数据如果人 select 全部来自下拉列表
那么我将如何更改此查询?
希望得到您的建议
为了获得所有行,你的 where 子句应该有
where (@status <> 0 AND pobox = @Status) OR (@status = 0 AND 1=1)
然而,第二个 OR 有一些冗余的过滤器,应该只有 @status= 0
where (@status <> 0 AND pobox = @Status) OR (@status = 0)
Pass 'ALL' if person select all from dropdown and write query like below:
select Zipcode, (CASE pobox
WHEN 1 THEN 'Available'
WHEN 2 THEN 'Reserved'
WHEN 3 THEN 'Assigned'
WHEN 4 THEN 'Closed'
END) as ' Status'
from custom_pobox
where CASE WHEN @Status <> 'ALL' THEN pobox=@Status ELSE 1=1 END
SELECT Zipcode, (CASE pobox
WHEN 1 THEN 'Available'
WHEN 2 THEN 'Reserved'
WHEN 3 THEN 'Assigned'
WHEN 4 THEN 'Closed'
END) as ' Status'
FROM custom_pobox
WHERE Status IN( CASE WHEN @ALL <> 1 THEN (SELCECT CASE pobox
WHEN 1 THEN 'Available'
WHEN 2 THEN 'Reserved'
WHEN 3 THEN 'Assigned'
WHEN 4 THEN 'Closed'
END) FROM CUSTOM_POBOX) ELSE @STATUS END)
我想按照我的查询所示明智地获取数据状态,
select Zipcode,
(CASE pobox
WHEN 1 THEN 'Available'
WHEN 2 THEN 'Reserved'
WHEN 3 THEN 'Assigned'
WHEN 4 THEN 'Closed'
END) as ' Status'
from custom_pobox
where pobox=@Status
假设@status = 0 或 1 或 2 任意值
但是我们在数据库中有从 0 到 4 的值我想显示所有数据如果人 select 全部来自下拉列表
那么我将如何更改此查询?
希望得到您的建议
为了获得所有行,你的 where 子句应该有
where (@status <> 0 AND pobox = @Status) OR (@status = 0 AND 1=1)
然而,第二个 OR 有一些冗余的过滤器,应该只有 @status= 0
where (@status <> 0 AND pobox = @Status) OR (@status = 0)
Pass 'ALL' if person select all from dropdown and write query like below:
select Zipcode, (CASE pobox
WHEN 1 THEN 'Available'
WHEN 2 THEN 'Reserved'
WHEN 3 THEN 'Assigned'
WHEN 4 THEN 'Closed'
END) as ' Status'
from custom_pobox
where CASE WHEN @Status <> 'ALL' THEN pobox=@Status ELSE 1=1 END
SELECT Zipcode, (CASE pobox
WHEN 1 THEN 'Available'
WHEN 2 THEN 'Reserved'
WHEN 3 THEN 'Assigned'
WHEN 4 THEN 'Closed'
END) as ' Status'
FROM custom_pobox
WHERE Status IN( CASE WHEN @ALL <> 1 THEN (SELCECT CASE pobox
WHEN 1 THEN 'Available'
WHEN 2 THEN 'Reserved'
WHEN 3 THEN 'Assigned'
WHEN 4 THEN 'Closed'
END) FROM CUSTOM_POBOX) ELSE @STATUS END)