如何查询int列中的任何值? [英] How do you query an int column for any value?

查看:82
本文介绍了如何查询int列中的任何值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何查询一列中的任何值? (即,我如何构建一个动态的where子句,该子句可以过滤值,也可以不过滤值.)

How can you query a column for any value in that column? (ie. How do I build a dynamic where clause that can either filter the value, or not.)

我希望能够查询特定值,也可以不查询.例如,我可能希望该值为1,但我可能希望其为任何数字.

I want to be able to query for either a specific value, or not. For instance, I might want the value to be 1, but I might want it to be any number.

是否可以使用通配符(例如"*")来匹配任何值,以便可以在不需要过滤器的地方动态插入通配符?

Is there a way to use a wild card (like "*"), to match any value, so that it can be dynamically inserted where I want no filter?

例如:

select int_col from table where int_col = 1  // Query for a specific value
select int_col from table where int_col = *  // Query for any value

我不想使用2条单独的SQL语句的原因是因为我将其用作SQL数据源,该数据源只能有1条select语句.

The reason why I do not want to use 2 separate SQL statements is because I am using this as a SQL Data Source, which can only have 1 select statement.

推荐答案

有时候我会查询实际值(例如1、2 ...),所以我也不会有条件.

Sometimes I would query for actual value (like 1, 2...) so I can't not have a condition either.

我认为您希望在WHERE子句上具有一些动态行为,而不必动态 build 您的WHERE子句.

I take it you want some dynamic behavior on your WHERE clause, without having to dynamically build your WHERE clause.

使用单个参数,您可以像这样使用ISNULL(或COALESCE):

With a single parameter, you can use ISNULL (or COALESCE) like this:

 SELECT * FROM Table WHERE ID = ISNULL(@id, ID)

允许NULL参数匹配所有内容.一些人更喜欢更长但更明确的东西:

which allows a NULL parameter to match all. Some prefer the longer but more explicit:

 SELECT * FROM Table WHERE (@id IS NULL) OR (ID = @id)

这篇关于如何查询int列中的任何值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆