gpt4 book ai didi

sql - 如何使用 dashDB 表中名为 _id 的列?

转载 作者:行者123 更新时间:2023-12-01 12:22:27 26 4
gpt4 key购买 nike

我在 Cloudant 中有一个数据库,其中的文档 ID 是 _id

将此数据从 Cloudant 复制到 dashDB 后,我有 2 个单独的表,我想使用此 _id 列加入它们。在运行 SQL 中,我尝试了以下操作,但这不会运行。我在这里错过了什么?我是否需要将列名 _id 替换为不带下划线的内容?

select m.title, m.year, g.value 
from MOVIES m
inner join MOVIES_GENRE g on m._ID = g._ID;

最佳答案

TL;DR:正如@gmiley 指出的那样,问题是由 _ID 列名引起的,它不是普通标识符(见下面的定义),因此需要在您的 SQL 语句中用双引号 "_ID" 或单引号 '_ID' 括起来。

select m.title, m.year, g.value 
from MOVIES m
inner join MOVIES_GENRE g on m."_ID" = g."_ID";

与普通标识符不同引号标识符区分大小写("_id""_ID" 不同,而titleTITLE 相同)。如果您要在语句中指定 "_id",则会引发错误,指示未找到该列。

自从您提到您使用了 Cloudant warehousing process要填充您的 DashDB 表,可能值得一提的是,在架构发现期间生成 DDL 时,属性名称是大写的。

示例:具有此结构的 JSON 文档的内容

 {
"_id": "000018723bdb4f2b06f830f676cfafd6",
"_rev": "1-91f98642f125315b929be5b5436530e7",
"date_received": "2016-12-04T17:46:47.090Z",
...
}

将映射到三列:

  • _ID 类型 VARCHAR(...)
  • _REV 类型 VARCHAR(...)
  • DATE_RECEIVED 类型 ...
  • ...

希望这对您有所帮助!


来自DB2 SQL reference :

An ordinary identifier is an uppercase letter followed by zero or more characters, each of which is an uppercase letter, a digit, or the underscore character. Note that lowercase letters can be used when specifying an ordinary identifier, but they are converted to uppercase when processed. An ordinary identifier should not be a reserved word.

Examples: WKLYSAL WKLY_SAL

A delimited identifier is a sequence of one or more characters enclosed by double quotation marks. Leading blanks in the sequence are significant. Trailing blanks in the sequence are not significant, although they are stored with the identifier. Two consecutive quotation marks are used to represent one quotation mark within the delimited identifier. In this way an identifier can include lowercase letters.

Examples: "WKLY_SAL" "WKLY SAL" "UNION" "wkly_sal"

关于sql - 如何使用 dashDB 表中名为 _id 的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42959717/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com