如果table-sql中存在特定值,则返回True

时间:2015-06-13 10:21:14

标签: sql ms-access select-query

我想创建一个SQL查询,如果特定列中存在特定值,它将返回True;如果没有,那么它将返回False。

我知道我可以创建像'SELECT something FROM somewhere WHERE something'这样的东西。在这种情况下,我不想选择任何东西,只是为了检查。

我的问题是我该怎么做。

5 个答案:

答案 0 :(得分:1)

您可以使用IIf function

SELECT IIf(something = 'Some value', True, False) FROM somewhere;

答案 1 :(得分:1)

在Access中,您可以使用DCount表达式计算某事字段包含'某些值'的行数。以下是从立即窗口复制的示例:

Debug.Print DCount("*", "somewhere", "something='some value'")
 1
Debug.Print DCount("*", "somewhere", "something='BOGUS'")
 0 

你可以调整它,当计数大于零时给你True,或者计数为零时给你False:

Debug.Print (DCount("*", "somewhere", "something='some value'") > 0)
True
Debug.Print (DCount("*", "somewhere", "something='BOGUS'") > 0)
False

如果你想从一个查询中做到这一点,这个将返回-1表示True,零表示False:

SELECT (DCount("*", "somewhere", "something='some value'") > 0) AS value_exists;

或者您可以使用Format表达式将这些值显示为字符串:“True”;或“假”

SELECT Format(DCount("*", "somewhere", "something='some value'") > 0, 'True/False') AS value_exists;

答案 2 :(得分:1)

顾名思义,DLookup就是为了这个:

SomevalueExists = Not IsNull(DLookup("Id", "somewhere", "somefield = somevalue"))

答案 3 :(得分:0)

试试这个:

select case when x is null then false else true end
from (select max(somecol) x
from sometable
where somecol = somevalue) a

答案 4 :(得分:0)

只需使用

select count(*) from tableName where columnName = '$variableInput';

如果您打算重复使用它,您也可以将它作为一个准备好的语句,您可以通过设计用于数据库的任何接口来调用它。如果返回的值大于零,则表示它为真,例如

    if(preparedStatement($variableInput)>0)
{
$flag = true;
}
else
{
$flag = false;
}
相关问题