Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 基于存储在另一个表中的列名获取值_Sql_Postgresql - Fatal编程技术网

Sql 基于存储在另一个表中的列名获取值

Sql 基于存储在另一个表中的列名获取值,sql,postgresql,Sql,Postgresql,我有以下表格: "DocumentSubject" ( "Id" SERIAL NOT NULL, "Description" text, "List1Left" text, "List1Right" text ); "DocumentRegistryAttributes" ( "Id" SERIAL NOT NULL, "DocumentSubjectId" integer, "Code" text, "WorkDetails" text, "Name"

我有以下表格:

"DocumentSubject"
(
  "Id" SERIAL NOT NULL,
  "Description" text,
  "List1Left" text,
  "List1Right" text
);

"DocumentRegistryAttributes"
(
  "Id" SERIAL NOT NULL,
  "DocumentSubjectId" integer,
  "Code" text,
  "WorkDetails" text,
  "Name" text
);
DocumentSubject
中有一个名为
List1Left
的列,其中包含
DocumentRegistryAttributes
表f.e.
code
中的列名称。 我是否可以根据
DocumentSubject
表中存储的字符串列名从
DocumentRegistryAttributes
中检索列
code
的值? 我需要这样的东西:

“DocumentRegistryAttributes”[“DocumentSubject”。“List1Left”]
您可以使用
to_jsonb()
“DocumentRegistryAttributes”
中的行创建一个JSON,列名作为键,然后从JSON中选择文本,其中键是
“DocumentSubject.”“List1Left”中的文本


不相关,但是:您应该真正避免那些可怕的引用标识符。从长远来看,它们比它们的价值要麻烦得多。实际上,您正在实施实体属性值设计。现在,如果将
DocumentRegistryAttributes
信息存储在
DocumentSubject
表中的
JSONB
列中,这将更加灵活(可能也更加高效)。我需要尝试这种方法,但我需要检索大量列,我不确定这样查询是否会更快,无论如何,这是一个很好的解决方案。谢谢@一匹没有名字的马:是的,没错。我编辑了答案。谢谢
SELECT *,
       to_jsonb(dra)->>ds."List1Left"
       FROM "DocumentSubject" ds
            LEFT JOIN "DocumentRegistryAttributes" dra
                      ON dra."DocumentSubjectId" = ds."Id";