简体   繁体   English

查询不会在 iif ms-access sql 中返回真值

[英]Query doesn't retrun true value in iif ms-access sql

I want to get RollNo if it is present when certain conditions meet otherwise it should return -1 .如果在某些条件满足时存在RollNo ,我想获得它,否则它应该返回-1 But in my case it is returning RollNo when its present, otherwise it returns nothing .但在我的情况下,当它出现时它会返回RollNo ,否则它会返回nothing

SQL is under:- SQL 在:-

SELECT iif(isnull(RollNo),-1,RollNo) from students where SName = "sf" and FName= "da" and 
DOB=#7/16/2020# and ClassID = 0

Hmm.唔。 . . . . If you always want to return exactly one row, then I would suggest aggregation:如果您总是想准确返回一行,那么我建议聚合:

SELECT NZ(MAX(RollNo), -1)
FROM students 
WHERE SName = "sf" and FName= "da" and DOB=#7/16/2020# and ClassID = 0;

If nothing matches the WHERE clause, then MAX(RollNo) returns NULL -- which is converted to a -1 by NZ() .如果没有任何内容与WHERE子句匹配,则MAX(RollNo)返回NULL - 由NZ()转换为-1

This returns exactly one row with field RollNo .这将返回包含字段RollNo的一行。

SELECT iif(isnull(max(RollNo)),-1,max(RollNo)) from students where SName = "sf" and
 FName= "da" and DOB=#7/16/2020# and ClassID = 0

if the conditions in where clause meet it return RollNo otherwise return -1如果 where 子句中的条件满足则返回RollNo否则返回-1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM