Postgresql 函数返回复合值 - 如何将复合值作为单独的列访问?

发布于 2024-09-24 16:38:22 字数 209 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红玫瑰 2024-10-01 16:38:22

使用:

SELECT * 
  FROM myfunc()

您可以阅读有关本文中的功能的更多信息

Use:

SELECT * 
  FROM myfunc()

You can read more about the functionality in this article.

め七分饶幸 2024-10-01 16:38:22

答案已被接受,但我想我应该将其放入:

它可能有助于思考数据的类型以及这些类型适合整个查询的位置。 SQL 查询本质上可以返回三种类型:

  • 单个标量值 值
  • 列表 值表

当然,列表只是一个单列表,而标量只是一个单值列表。)

当您查看通过这些类型,您会看到 SQL SELECT 查询具有以下模板:

SELECT scalar(s)
FROM table
WHERE boolean-scalar

如果您的函数或子查询返回一个表,则它属于 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:

  • A single scalar value
  • A list of values
  • A table of values

(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:

SELECT scalar(s)
FROM table
WHERE boolean-scalar

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文