Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.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/2/ajax/6.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输入值可以是单个值,也可以是多个值 在SQL查询中,如果输入值为0,则取输入,因为列的所有值仅考虑输入值;_Sql - Fatal编程技术网

SQL输入值可以是单个值,也可以是多个值 在SQL查询中,如果输入值为0,则取输入,因为列的所有值仅考虑输入值;

SQL输入值可以是单个值,也可以是多个值 在SQL查询中,如果输入值为0,则取输入,因为列的所有值仅考虑输入值;,sql,Sql,假设输入城市数=0,则对所有城市数(即1到50)运行查询,否则对输入城市数(如5)运行查询。。如何编写此代码 if the City_num = 0 then Select * from emp where City_num in 1to 50 if the City_num = 5 then Select * from emp where City_num = 5 ) 添加此WHERE语句: WHERE city_num = ? OR 0 = ? 如果通过0,则等于: WHERE cit

假设输入城市数=0,则对所有城市数(即1到50)运行查询,否则对输入城市数(如5)运行查询。。如何编写此代码

if the City_num = 0 then Select * from emp where City_num in  1to 50
if the City_num = 5 then Select * from emp where City_num = 5
)

添加此WHERE语句:

WHERE city_num = ? OR 0 = ?
如果通过0,则等于:

WHERE city_num = 0 OR 0 = 0
WHERE city_num = 5 OR 0 = 5
由于0=0始终为真,因此它将获取所有行。 如果通过5,则等于:

WHERE city_num = 0 OR 0 = 0
WHERE city_num = 5 OR 0 = 5
由于0=5总是FALSE,那么它将只获取city_num=5的行

试试这个:

DECLARE @input INT = 2;
SELECT * FROM TABLE1
WHERE (city_num= @input AND @input > 0) OR @input = 0;

我假设city_num是整数。如果varchar

提示:使用CASE语句或union这是非常模糊的。您能否提供示例数据、预期输出和描述您想要使用的查询。Forpas有更好的答案