从sql查询中获取列别名

时间:2014-05-06 18:04:29

标签: postgresql

来自这样的查询:

select
        aaa.rank as my_rank,
        aa.*,
        aa.article_category
from
        articles.article_article_authors as aaa
        left join article_article as aa on aa.id = aaa.article_id
;

我想获取列别名 - 在这个特殊情况下,列my_rank的别名为aaa.rank。不幸的是,使用EXPLAIN使用任何选项都无法提供我需要的信息:

[
  {
    "Plan": {
      "Node Type": "Nested Loop",
      "Join Type": "Left",
      "Startup Cost": 0.00,
      "Total Cost": 2.66,
      "Plan Rows": 1,
      "Plan Width": 70,
      "Output": ["aaa.article_id", "aaa.user_id", "aaa.rank", "aa.id", "aa.creation_time", "aa.pub_start", "aa.pub_end", "aa.is_visible", "aa.rank", "aa.answer_to_article_id", "aa.title", "aa.hits", "aa.meta_keywords", "aa.content", "aa.archived", "aa.article_category", "aa.article_category"],
      "Join Filter": "(aa.id = aaa.article_id)",
      "Plans": [
        {
          "Node Type": "Seq Scan",
          "Parent Relationship": "Outer",
          "Relation Name": "article_article_authors",
          "Schema": "public",
          "Alias": "aaa",
          "Startup Cost": 0.00,
          "Total Cost": 1.01,
          "Plan Rows": 1,
          "Plan Width": 12,
          "Output": ["aaa.id", "aaa.article_id", "aaa.user_id", "aaa.rank"]
        },
        {
          "Node Type": "Seq Scan",
          "Parent Relationship": "Inner",
          "Relation Name": "article_article",
          "Schema": "public",
          "Alias": "aa",
          "Startup Cost": 0.00,
          "Total Cost": 1.29,
          "Plan Rows": 29,
          "Plan Width": 58,
          "Output": ["aa.id", "aa.creation_time", "aa.pub_start", "aa.pub_end", "aa.is_visible", "aa.rank", "aa.answer_to_article_id", "aa.title", "aa.hits", "aa.meta_keywords", "aa.content", "aa.archived", "aa.article_category"]
        }
      ]
    }
  }
]

有没有办法使用explain获取列别名,还是应该遵循其他路径?当然我可以解析查询字符串然后获取我的列别名,但我更喜欢postgres为我做这个。

1 个答案:

答案 0 :(得分:1)

我使用正则表达式来匹配SQL字符串的所有列/别名。我想我已经涵盖了大多数情况,但我可能错过了一些。

(?:              # Start non-capturing group
  select         # Match "select" literally
 |               # OR
  \G             # Match the end of the previous match (our last column)
)                # End non-capturing group
\s+              # Catch extra whitespace
\K               # Remove everything to the left from the match
(?:              # Start non-capturing group
  (?:            # Start non-capturing group
    .*?          # Lazily match column (this can be modified to your needs)
    \s+as\s+     # Match " as " literally
    ([^,]+)      # Capture the alias (anything but a comma)
    ,?           # Optional comma
  )              # End non-capturing group
 |               # OR
  ([^,]+)        # Capture the column (anything but a comma)
  ,?             # Optional comma
)                # End non-capturing group
(?=              # Start lookahead
  .*?            # Lazily match characters (whitespace)
  \bfrom\b       # Up to "from" literally (with word delimiters)
)                # End lookahead

这将捕获第一组中的别名和第二组中的列,整个字符串将匹配。如果你开始有疯狂的列别名,它可能会有问题,但似乎匹配任何基本的中间。注意我使用修饰符gsi表示全局,点匹配换行符和不区分大小写。

Regex101