如何查询字段是否不在数据库中

时间:2019-02-13 13:34:23

标签: sql ms-access

我需要选择该字段不在数据库中的查询。

SELECT s.buyer as [Buyer],
SUM(s.total) as [Total Sales],
b.balance,s.buydate
from salesdb s inner join buyerdb b on s.buyer=b.buyer
where 
(s.buydate=@buydate and
b.balance>@balance and s.total & "" = "" or s.total is NULL or s.total=@empty ) or (s.buydate=@buydate and b.balance=@balance and s.total>@sales)
or (s.buydate=@buydate and b.balance>@balance and s.total>@sales) or 
(s.buydate is NULL and s.total & "" = ""and b.balance>@balance and s.total & "" = "" or s.total is NULL or s.total=@empty )
GROUP By s.buyer,b.balance,s.buydate"
我正在查询同一表中但尚未在数据库中的日期和总销售额。但只会显示来自另一个表的买方及其余额,只有两个表中的买方名称相同。

其中s.buydate为null?
s.buydate =“” =“”

它几乎可以正常工作,我只需要知道如何查询数据库中是否还没有日期即可。 尝试过,但是没有用

1 个答案:

答案 0 :(得分:0)

s.buydate IS NULL是正确的。但是您还有其他问题。 AND的优先级高于OR。这意味着

s.buydate=@buydate AND
b.balance>@balance AND
s.total & "" = "" OR s.total IS NULL OR s.total=@empty

等同于

(s.buydate=@buydate AND b.balance>@balance AND s.total & "" = "") OR
s.total IS NULL OR
s.total=@empty

这不是您想要的。

我不清楚@empty应该是什么。在数据库中,字符串可以是空字符串("")或NullEmpty值仅适用于VBA Variants,不适用于SQL。由于在SQL中可以将单引号用作字符串定界符,因此请写

(s.buydate = @buydate OR s.buydate IS NULL) AND
b.balance > @balance AND
(s.total = '' OR s.total IS NULL)

这将返回具有必需购买日期或尚无购买日期的记录。如果不仅缺少该字段,请保留整个记录,然后必须使用LEFT JOIN而不是INNER JOIN。