简体   繁体   English

如何在 Access 查询中检查变量是否为空并为其分配值(如果是)?

[英]How do I check if a variable is null and assign it a value if it is, in an Access query?

I'm not a big Access user, but I need to work with it from time to time.我不是 Access 的大用户,但我需要不时使用它。 Now I have run into a, for me, completely new task with Access.现在,我遇到了一个全新的 Access 任务。 I can solve this type of problem with SQL in MS SQL Server.我可以在 MS SQL Server 中使用 SQL 解决此类问题。 But the SQL syntax in Access doesn't recognise my SQL code at all so I guess it's quite different from "regular" SQL.但是 Access 中的 SQL 语法根本无法识别我的 SQL 代码,所以我猜它与“常规” SQL 有很大不同。

Anyway, I have a table that needs to be sorted over some attributes.无论如何,我有一个需要对某些属性进行排序的表。 For this question we can take two and call them A1 and A2 .对于这个问题,我们可以取两个并将它们称为A1A2 A1 doesn't have any null values, A2 does. A1没有任何空值, A2有。 The sort order is on A2 :排序顺序在A2

  • If A2 has a value, sort by it.如果A2有值,则按它排序。
  • If A2 is null, then sort by the value from A1 .如果A2为空,则按A1的值排序。

I have tried so many different ways to solve this and all of them has failed.我尝试了很多不同的方法来解决这个问题,但都失败了。 It's absolutely clear that my knowledge in Access is limited so I guess that I'm approaching this in a faulty way.很明显,我在 Access 方面的知识是有限的,所以我想我是在以错误的方式处理这个问题。

Use NZ() :使用NZ()

ORDER BY NZ(A2, A1)

NZ returns the second parameter if the first is null, otherwise it returns the first parameter.如果第一个参数为空,NZ 返回第二个参数,否则返回第一个参数。

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

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