为什么在访问函数返回的数组(在SELECT中)时需要添加括号? [英] why is it necessary to add parenthesis when accessing an array returned by a function (in SELECT)?

查看:82
本文介绍了为什么在访问函数返回的数组(在SELECT中)时需要添加括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自(脚本)编程语言,我不明白为什么起作用

Coming from (script) programming languages, I do not understand why this does not work

SELECT regexp_matches('hello world', '(he).*(wo)', 'g')[1];

,同时添加附加括号可以起作用(至少在Postgres中有效)。

while adding additional parenthesis does work (at least in Postgres).

SELECT (regexp_matches('hello world', '(he).*(wo)', 'g'))[1];

我会假设 regexp_matches ,但是我想我对SQL的语法缺乏了解。

I would have assumed that the array returned by regexp_matches is accessed in both cases, but I guess my syntax understanding of SQL is lacking here.

那为什么我们需要括号?

推荐答案

括号中的内容非常高优先级,因此您的查询被解析为

the brackets have a pretty high precedence , so your query is parsed as

SELECT regexp_matches(('hello world', '(he).*(wo)', 'g')[1]);

这没有意义,您想在调用<$ c的结果上应用索引$ c> regexp_matches(),因此您必须添加括号以将regexp_matches调用分组。

This doesn't make sense, you want to apply the index on the result of calling regexp_matches() , so you have to add parenthesis to group the regexp_matches call.

(请注意,这两个字符[]括在方括号中,您添加了括号,而不是括号

(Note these two characters [] are brackets, you added parenthesis, not brackets

这篇关于为什么在访问函数返回的数组(在SELECT中)时需要添加括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆