简体   繁体   English

如何将 TRIM 的结果与表中的列匹配以进行 JOIN

[英]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 列相关联
  • the first uid in the set_of_guids matches the vm_owner and also the id column in the nodes table and has a node_name in another column in nodes set_of_guids 中的第一个 uid 与 vm_owner 以及节点表中的 id 列匹配,并且在节点的另一列中有一个 node_name

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子句中定义列——然后它可以在SELECTFROMWHERE中的表达式中使用,就像任何其他列一样。

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.

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