[英]How to match results of TRIM to column in table for a JOIN
See the original question and code further down.进一步查看原始问题和代码。 Here is the code that works:
这是有效的代码:
SELECT v1.s_owner_guid,
n.node_name as s_owner_name
FROM vm_list v
CROSS JOIN LATERAL (
SELECT (VALUES (TRIM(split_part(v2.set_of_guids, ':', 3), '|')))
) v1(s_owner_guid)
LEFT JOIN nodes n
ON n.id = v1.s_owner_guid::uuid
Original code that does not work:不起作用的原始代码:
SELECT
(TRIM (split_part(v2.set_of_guids, ':', 3)), '|') AS s_owner_guid
, n1.node_name as s_owner_name
FROM vm_list as v2
INNER JOIN nodes
AS n1
ON n1.id=s_owner_guid
I want to get s_owner_guid and s_owner_name out of this SELECT我想从这个 SELECT 中得到 s_owner_guid 和 s_owner_name
This is a part of the query that I am having issues with and is a nested SELECT这是我遇到问题的查询的一部分,是嵌套的 SELECT
The error I get is:
ERROR: column "s_owner_guid" does not exist
LINE 7: ON v2.owner=s_owner_guid
I have a set_of_guids containing 2 node uids seperated by certain characters我有一个 set_of_guids 包含由某些字符分隔的 2 个节点 uid
I have 2 tables with the information I need我有 2 个表格,其中包含我需要的信息
vm_list
and nodes are the 2 tables vm_list
和节点是 2 个表vm_owner_guid
is a column in vm_list table that has the uid matching id column in nodes table vm_owner_guid
是 vm_list 表中的一列,其 uid 与节点表中的 id 列匹配id
is a column in the nodes table id
是节点表中的一列vm_name
is a column in vm_list with the VM name vm_name
是 vm_list 中带有 VM 名称的列node_name
is a column in nodes with that is associated with the id column in nodes table node_name
是节点中的一列,与节点表中的 id 列相关联 I need to parse the set_of_nodes to get the second uid for the VM so I am using the SPLIT_PART and TRIM options to isolate the uid and am calling it s_owner_guid我需要解析 set_of_nodes 以获得 VM 的第二个 uid,因此我使用 SPLIT_PART 和 TRIM 选项来隔离 uid 并将其称为 s_owner_guid
I need to take s_owner_guid and match it against id in the nodes table to get the second node_name我需要获取 s_owner_guid 并将其与节点表中的 id 进行匹配以获得第二个 node_name
Ultimately the output will look something like this:最终 output 看起来像这样:
vm_name | p_owner_guid | p_owner_name | s_owner_guid | s_owner_name
-------------+---------------------------------------+-------------------+---------------------------------------+-----------------------
NAMEOFVM1 | b2a0bb4e-0a6a-4208-8ff1-6df549cf9c3f | primary_node_name | 1c732242-56d4-c9c8-d275-ba271600c314 | secondary_node_name
I can get the first 4 fields now, but not the last s_owner_name.我现在可以获取前 4 个字段,但不能获取最后一个 s_owner_name。 Is there any way to accomplish this?
有什么办法可以做到这一点? I am trying not to create a temporary table due to having a live system depending on the DB.
由于有一个依赖于数据库的实时系统,我试图不创建临时表。
Postgres supports lateral joins, which are a nice way of accomplishing this: Postgres 支持横向连接,这是实现此目的的好方法:
SELECT v.s_owner_guid,
n.node_name as s_owner_name
FROM vm_list v2 CROSS JOIN LATERAL
(VALUES (TRIM(split_part(v2.set_of_guids, ':', 3), '|')))
) v(s_owner_guid)
nodes n
ON n.id = v.s_owner_guid;
A lateral join allows you to define the column in the FROM
clause -- it can then be used in expressions in the SELECT
, FROM
, or WHERE
, just like any other column.横向连接允许您在
FROM
子句中定义列——然后它可以在SELECT
、 FROM
或WHERE
中的表达式中使用,就像任何其他列一样。
You can't use a column alias on the same level where you introduced it.您不能在引入它的同一级别上使用列别名。 You need to wrap the query in a derived table:
您需要将查询包装在派生表中:
SELECT v2.s_owner_guid,
n1.node_name as s_owner_name
FROM (
select trim(split_part(v2.set_of_guids, ':', 3)), '|') AS s_owner_guid
FROM vm_list
) as v2
JOIN nodes AS n1 ON n1.id = s_owner_guid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.