0

假设我有一个房地产网站,并且在默认页面中,我想让用户获得符合他的参数的帖子数量

我添加 了一个示例图像的链接

计数器应该对每次更改做出反应并自动向用户显示数字(这将在 ajax jquery 中执行)

我写了一个查询,但只有在填写了所有参数后它才会起作用,并且我希望能够填写一些并且仍然让计数器工作

这是查询

ALTER PROCEDURE shahar30_nadlan.CountPropertyResultsByquery 


@AreaID         int,
@DealID        int,
@PropertyTypeId int,
@FieldMax      int,
@FieldMin      int,
@RoomsMax      decimal,
@RoomsMin      decimal,
@PriceMax      int,
@PriceMin      int,
@ParkingSpace  bit,
@Elevator      bit,
@Aircondition  bit,
@Furniture     bit,
@Warehouse     bit,
@Balkony       bit,
@handicapped  bit, 
@Immediate    bit,
@Description  NVARCHAR(MAX)

AS
BEGIN


select count(*) from Tbl_Property
where AreaID        = @AreaID         and 
  DealID        = @DealID         and
  PropertyTypeId= @PropertyTypeId and
  Field  <= @FieldMax        and
  Field  >= @FieldMin        and
  Rooms  <= @RoomsMax        and
  Rooms  >= @RoomsMin        and
  Price  <= @PriceMax        and
  Price  >= @PriceMin       and
  ParkingSpace = @ParkingSpace   and
  Elevator = @Elevator       and
  Aircondition = @Aircondition   and
  Furniture = @Furniture      and
  Warehouse = @Warehouse      and
  Balkony = @Balkony        and
  handicapped = @handicapped    and
  [Immediate] = @Immediate      and
  Description like  '%' + @Description + '%' 

END

谢谢你,沙哈尔·纳迪亚。

4

2 回答 2

2

并非 所有参数都应填写

如果未提供值,则其为空(除非它具有默认值),然后:

您应该使用的模式是:

select * from myTable where 

(  @companyId  IS NULL  OR ( companyId = @companyId ))
and
 ( @Name IS NULL  OR ( Name  = @Name ))
and
...

或者

 (   companyId  = isnull(@companyId,companyId ))
      and
  (    Name = isnull(@Name ,Name ))
      and

    ...

这里也可以使用 coalesce - 但速度较慢。(比为空)

于 2012-04-25T09:04:16.733 回答
2

你试过可选参数吗?这是一个例子:

@PriceMin      int = 1,

您可以像这样设置默认值,然后只传递您想要的参数,其余的则采用默认值。

于 2012-04-25T09:09:40.253 回答