Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sql where语句帮助。如果为0,则选择全部_Sql_Where Clause - Fatal编程技术网

sql where语句帮助。如果为0,则选择全部

sql where语句帮助。如果为0,则选择全部,sql,where-clause,Sql,Where Clause,我有一个国家标签,它定义了你来自哪个国家,1-5。。我已经写了一个投递列表,让你选择你想搜索的巫婆国家。我在下拉列表中有一个部分,我想选择“全部搜索”。如果country是0,那么我希望它不受限制,就像下面的代码一样,其中(country=@country和town=@town)我只希望它是(town=@town)等等 当设置“选择所有国家”选项时,您可以只为@country发送空值,然后 使用where子句,如下所示: 其中(@country为空或country=@country) 编辑:只是

我有一个国家标签,它定义了你来自哪个国家,1-5。。我已经写了一个投递列表,让你选择你想搜索的巫婆国家。我在下拉列表中有一个部分,我想选择“全部搜索”。如果country是0,那么我希望它不受限制,就像下面的代码一样,其中(country=@country和town=@town)我只希望它是(town=@town)等等


当设置“选择所有国家”选项时,您可以只为@country发送空值,然后

使用where子句,如下所示: 其中(@country为空或country=@country)


编辑:只是为了澄清一些事情。我之所以使用空值是因为imho表示“该参数没有筛选器”比使用任意值要好。

如果我理解正确,您传递的@Country变量可能值为1-5,但也可能值为0,等于所有国家

尝试使用类似以下内容:

IF @Case = 1 
  BEGIN 
      SELECT REC_ID, 
             COUNTRY, 
             POSTALCODE, 
             TOWN, 
             LAT, 
             LNG 
      FROM   DBO.TBL_POSTALCODE AS PC WITH (NOLOCK) 
      WHERE  ( COUNTRY = @country 
                OR @country = 0 ) 
             AND ( TOWN = @town 
                    OR POSTALCODE = @postalcode ) 
  END 

您应该能够在
中添加另一个条件,其中

...
WHERE
  (@selectAllCountries = 1 OR country = @country)
  AND (town = @town OR postalcode = @postalcode)
很明显,我假设您有一个
@selectAllCountries
变量,该变量为1或0。

将其设置为工作设置(country=null或town=@town)或(country=null或postalcode=@postalcode)


谢谢你的帮助

(country=null和town=@town)像这样吗?不,比如WHERE(@country=null或country=@country)和(town=@town或postalcode=@postalcode)我让它工作了(country=null或town=@town)或者(country=null或postalcode=@postalcode)@PatrikChristoffersson,你在用什么关系数据库管理系统?通常写“=null”不是正确的方法。如果这为您解决了问题,您应该接受您自己的答案(答案左侧有“V”图标),并对您认为有用的任何其他答案进行投票。谢谢
...
WHERE
  (@selectAllCountries = 1 OR country = @country)
  AND (town = @town OR postalcode = @postalcode)