在 Access-SQL 中将 NULL 值设置为自定义值
当LEFT JOINing两个表时,有没有办法将无法匹配的单元格(NULL)设置为自定义值?因此,例如,当结果返回时,NULL 单元格实际上有一个值,例如“N/A”或“未找到”?
我想在 MS Access 2003
示例中执行此操作:
| id | value | | id | other value |
|----|-------| LEFT JOIN |----|-------------|
| 1 | hello | -- id --> | 2 | world |
| 2 | you |
results in:
| id | value | other value |
| 1 | hello | NULL |
| 2 | you | world |
but should be:
| id | value | other value |
| 1 | hello | custom-val |
| 2 | you | world |
When LEFT JOINing two tables, is there a way to set the cells which can not be matched (NULL) to a custom value? So e.g. when the result returns, the NULL-cells actually HAVE a value, e.g. "N/A" or "Not found"?
I want to do this in MS Access 2003
Example:
| id | value | | id | other value |
|----|-------| LEFT JOIN |----|-------------|
| 1 | hello | -- id --> | 2 | world |
| 2 | you |
results in:
| id | value | other value |
| 1 | hello | NULL |
| 2 | you | world |
but should be:
| id | value | other value |
| 1 | hello | custom-val |
| 2 | you | world |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
Nz()
用任意值替换NULL
;将返回字段
F
的值,如果F
为NULL
,则返回“Not Present”
。You can use
Nz()
to substitute an arbitrary value for aNULL
;Would return either the value of field
F
, or"Not Present"
ifF
wereNULL
.请记住,SQL 的外连接是一种关系联合,它被明确设计为投影空值。您希望避免使用 null 值(在我看来,这也是一件好事),因此您应该避免使用外连接。请注意,现代关系语言已经完全放弃了空连接和外连接的概念(参见尾注)。
这个外连接:
…在语义上等同于这个 SQL 代码:
第二个查询可能看起来很冗长,但这只是因为 SQL 设计/演变的方式。上面只是自然连接、并集和半连接。但是,SQL 没有半连接运算符,如果您的产品尚未实现标准 SQL 的 NATURAL,则需要您在 SELECT 子句中指定列列表并编写 JOIN 子句JOIN 语法(Access 没有),这会导致使用大量代码来表达非常简单的内容。
因此,您可以编写类似于上面第二个查询的代码,但使用实际的默认值而不是空值。
镇上唯一的关系游戏是 Chris Date 和 Hugh Darwen 称为“第三宣言”的 D 语言规范。它明确拒绝 Codd 的空值(后来 Codd 提出了两种空值),不适应外连接运算符(在最近的著作中,作者提出了关系值属性作为外连接的替代方案)。具体引用:
CJ Date (2009):SQL 和关系理论:如何编写准确的 SQL 代码:第 4 章,“关于外连接的评论”(p.84)
Darwen, Hugh (2003):列名的重要性:“请注意,在教程 D 中,唯一的‘连接’运算符称为 JOIN,它的意思是‘自然连接’。” (第 16 页)
CJ Date 和 Hugh Darwen(2006):数据库、类型和关系模型:第三宣言:禁令 4:“D 不应包含‘关系’的概念,其中某些‘元组’包括某些‘属性’ ’这没有价值。”
Bear in mind that SQL’s outer join is a kind of relational union which is explicitly designed to project null values. You want to avoid using the null value (a good thing too, in my opinion), therefore you should avoid using outer joins. Note that modern relational languages have dispensed with the concept of null and outer join entirely (see endnote).
This outer join:
…is semantically equivalent to this SQL code:
The second query may look long winded but that’s only because of the way SQL has been designed/evolved. The above is merely a natural join, a union and a semijoin. However, SQL has no semijoin operator, requires you to specify column lists in the
SELECT
clause and to writeJOIN
clauses if your product hasn’t implemented Standard SQL’sNATURAL JOIN
syntax (Access hasn’t), which results in a lot of code to express something quite simple.Therefore, you could write code such as the second query above but using an actual default value rather than the null value.
The only relational game in town is the specification of a D language know as "The Third Manifesto" by Chris Date and Hugh Darwen. It explicitly rejects Codd's nulls (latterly Codd proposed two kinds of null) doesn't accommodate an outer join operator (in more recent writings the authors have proposed relation-valued attributes as an alternative to outer join). Specific citations:
C. J. Date (2009): SQL and Relational Theory: How to Write Accurate SQL Code: Ch 4, 'A remark on outer join' (p.84)
Darwen, Hugh (2003): The Importance of Column Names: "Note that in Tutorial D, the only 'join' operator is called JOIN, and it means 'natural join'." (p.16)
C. J. Date and Hugh Darwen (2006): Databases, Types and the Relational Model: The Third Manifesto: Proscription 4: "D shall include no concept of a 'relation' in which some 'tuple' includes some 'attribute' that does not have a value."