Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.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 从column=anything的表中选择_Sql_Sql Server - Fatal编程技术网

Sql 从column=anything的表中选择

Sql 从column=anything的表中选择,sql,sql-server,Sql,Sql Server,我需要的一个简化示例: 表“Transport”有两列;'“车辆”和“颜色”。用户可以按车辆、颜色或两者筛选记录 variable vehicle_choice = user_selected_vehicle (selected from a dropdown for example) variable colour_choice = user_selected_colour If user_selected_vehicle = nothing selected Then vehicl

我需要的一个简化示例:

表“Transport”有两列;'“车辆”和“颜色”。用户可以按车辆、颜色或两者筛选记录

variable vehicle_choice = user_selected_vehicle (selected from a dropdown for example)
variable colour_choice = user_selected_colour

If user_selected_vehicle = nothing selected Then
    vehicle_choice = *
End if

If user_selected_colour = nothing selected Then
    colour_choice = *
End if

Select query = ("Select * From Transport Where Vehicle = vehicle_choice And Colour = colour_choice")
因此,如果用户想要“红色”总线,则查询将如下所示:

("Select * From Transport Where Vehicle = 'bus' And Colour = 'red'")
("Select * From Transport Where Vehicle = * and Colour = 'yellow'")
那就好了,如果有记录的话可以找到

但是,如果用户希望所有车辆均为黄色,则查询如下:

("Select * From Transport Where Vehicle = 'bus' And Colour = 'red'")
("Select * From Transport Where Vehicle = * and Colour = 'yellow'")

很明显,这是一个虚构的语法,但这是我想要的,在SQL中可能吗?(使用MS SQL Server 2008)

您可以准备第二次查询

("Select * From Transport Where Vehicle = * and Colour = 'yellow'")
对此

("Select * From Transport Where Vehicle = Vehicle and Colour = 'yellow'")

这是解决问题的唯一办法。

您可以准备第二个查询

("Select * From Transport Where Vehicle = * and Colour = 'yellow'")
对此

("Select * From Transport Where Vehicle = Vehicle and Colour = 'yellow'")

这是解决问题的唯一办法。

这里有两种典型的解决方法。假设用户输入在变量
@vehicle
@color
中:

where (vehicle = @vehicle or @vehicle is null) and
      (colour = @colour or @colour is null)
这种方法的问题在于索引的使用。索引策略在
中很难使用。因此,如果要动态构造查询,最好只添加所需的子句:

@where = '1 = 1' +
         (case when @vehicle is not null then ' and vehicle = @vehicle' else '' end) +
         (case when @colour is not null then ' and colour = @colour' else '' end);

这里有两种典型的解决方法。假设用户输入在变量
@vehicle
@color
中:

where (vehicle = @vehicle or @vehicle is null) and
      (colour = @colour or @colour is null)
这种方法的问题在于索引的使用。索引策略在
中很难使用。因此,如果要动态构造查询,最好只添加所需的子句:

@where = '1 = 1' +
         (case when @vehicle is not null then ' and vehicle = @vehicle' else '' end) +
         (case when @colour is not null then ' and colour = @colour' else '' end);

你可以试试类似的东西

("Select * From Transport Where Vehicle like '%' and Colour = 'yellow'")

你可以试试类似的东西

("Select * From Transport Where Vehicle like '%' and Colour = 'yellow'")

对。这是可能的。只需按如下方式修改查询:

假设您的下拉参数为:

ddl1.val -- for transport
ddl2.val -- for color
现在,假设“全选”选项的默认ddl值为“0”或任何值。现在,您的查询将为:

 Select * From Transport Where (Vehicle = ddl1.val or ddl1.val='0') and
(Colour = ddl2.val or ddl2.val='0')
另一种方法是:

Select * From Transport Where Vehicle like '%' and Colour = 'color_selected'

如果您还需要关于这方面的任何其他澄清,请告诉我。

是的。这是可能的。只需按如下方式修改查询:

declare @vehicle_choice nvarchar(255)
declare @colour_choice nvarchar(255)
set @colour_choice = 'bus'
set @colour_choice = 'blue'

Select * From Transport 
    where 1= 1 
    and Vehicle like (case when @vehicle_choice Is null or @vehicle_choice = '' then '%' else @vehicle_choice end)
    and Colour like (case when @colour_choice Is null or @colour_choice = '' then '%' else @colour_choice end)   
假设您的下拉参数为:

ddl1.val -- for transport
ddl2.val -- for color
现在,假设“全选”选项的默认ddl值为“0”或任何值。现在,您的查询将为:

 Select * From Transport Where (Vehicle = ddl1.val or ddl1.val='0') and
(Colour = ddl2.val or ddl2.val='0')
另一种方法是:

Select * From Transport Where Vehicle like '%' and Colour = 'color_selected'


如果您还想对此进行任何澄清,请告诉我。

Erland Sommarskog写了一篇相当不错的文章,标题涵盖了大多数情况。此外,我建议您阅读“SQL in the Wild”(野生SQL)博客。我准备了解更多信息,当然会看看这些文章,谢谢。Erland Sommarskog写了一篇相当不错的文章,标题涵盖了大多数场景。此外,我建议阅读“野生SQL”博客。我想了解更多,当然会看看这些文章,谢谢。我的问题是“column=*”不起作用。我需要知道“column can equal anything”的正确语法是什么,这是否意味着column=column?@Ryan我给出了上面的解决方案:)哇,这是一个非常优雅的解决方案,比其他3个答案要好得多(y)我的问题是“column=*”不起作用。我需要知道“column can equal anything”(列可以等于任何东西)的正确语法是什么?这意味着column=column吗?@Ryan我给出了上面的解决方案:)哇,这是一个非常优雅的解决方案,比其他3个答案要好得多(y)啊,这很有意义。我现在就试试回来。好的,这很好,正是我需要的。非常感谢。很快就会接受的。啊,这是有道理的。我现在就试试回来。好的,这很好,正是我需要的。非常感谢。很快就会接受。这似乎也能奏效,而且是一个简单的解决方案。我只需要更改查询,使所有列参数都是“like”而不是“=”,并将null值更改为“%”。这似乎也可行,而且是一个简单的解决方案。我只需要更改查询,使所有列参数都是“like”而不是“=”,并将null值更改为“%”。
declare @vehicle_choice nvarchar(255)
declare @colour_choice nvarchar(255)
set @colour_choice = 'bus'
set @colour_choice = 'blue'

Select * From Transport 
    where 1= 1 
    and Vehicle like (case when @vehicle_choice Is null or @vehicle_choice = '' then '%' else @vehicle_choice end)
    and Colour like (case when @colour_choice Is null or @colour_choice = '' then '%' else @colour_choice end)