Postgresql 函数返回复合值 - 如何将复合值作为单独的列访问?
我有一个 Postgresql 函数,它返回定义为 (location TEXT, id INT) 的复合类型。当我运行“SELECT myfunc()”时,我的输出是一列文本类型,格式为:
("locationdata", myid)
这非常糟糕。有没有办法选择我的组合,以便我返回 2 列 - 一个 TEXT 列和一个 INT 列?
I have a Postgresql function which returns a composite type defined as (location TEXT, id INT). When I run "SELECT myfunc()", My output is a single column of type text, formatted as:
("locationdata", myid)
This is pretty awful. Is there a way to select my composite so that I get 2 columns back - a TEXT column, and an INT column?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用:
您可以阅读有关本文中的功能的更多信息。
Use:
You can read more about the functionality in this article.
答案已被接受,但我想我应该将其放入:
它可能有助于思考数据的类型以及这些类型适合整个查询的位置。 SQL 查询本质上可以返回三种类型:
当然,列表只是一个单列表,而标量只是一个单值列表。)
当您查看通过这些类型,您会看到 SQL SELECT 查询具有以下模板:
如果您的函数或子查询返回一个表,则它属于 FROM 子句。如果它返回一个列表,它可以进入 FROM 子句,也可以与 IN 运算符一起使用,作为 WHERE 子句的一部分。如果它返回标量,则它可以进入 SELECT 子句、FROM 子句或 WHERE 子句中的布尔谓词。
这是 SELECT 查询的不完整视图,但我发现它有助于弄清楚我的子查询应该去哪里。
Answer has already been accepted, but I thought I'd throw this in:
It may help to think about the type of the data and where those types fit into an overall query. SQL queries can return essentially three types:
(Of course, a list is just a one-column table, and a scalar is just a one-value list.)
When you look at the types, you see that an SQL SELECT query has the following template:
If your function or subquery is returning a table, it belongs in the FROM clause. If it returns a list, it could go in the FROM clause or it could be used with the IN operator as part of the WHERE clause. If it returns a scalar, it can go in the SELECT clause, the FROM clause, or in a boolean predicate in the WHERE clause.
That's an incomplete view of SELECT queries, but I've found it helps to figure out where my subqueries should go.