将值插入到一个查询的多行中

发布于 2024-10-18 04:18:28 字数 502 浏览 1 评论 0原文

这并不是主题所暗示的另一个蹩脚的问题;)。这是我的查询:

在提交时使用 (OIDS) 创建临时表 temp_tab AS SELECT 22 AS num, 'smth' AS Something_else;

SELECT * FROM temp_tab;

我想要完成的是一次向此临时表插入多个值,如下所示:

在提交时创建临时表 temp_tab DROP AS SELECT (22, 23, 24) AS num, ('smth', 'wqer', 'asdf') AS Something_else;

我不将数据插入普通表而不是临时表的原因是因为我想使用我的数据库来计算地理点(postgis)之间的距离,并且我有很多数据 - 这样做(加排序)对于 php 来说太详尽了,我不需要存储这些数据 - 我只想进行一些计算并将集合返回到我的代码。

It's not another lame question as the topic suggests ;). So here's my query:


CREATE TEMP TABLE temp_tab WITH (OIDS) ON COMMIT DROP AS SELECT 22 AS num, 'smth' AS something_else;

SELECT * FROM temp_tab;

What I'm trying to accomplish is to insert into this temporary table more than one value at a time, like this:


CREATE TEMP TABLE temp_tab WITH (OIDS) ON COMMIT DROP AS SELECT (22, 23, 24) AS num, ('smth', 'wqer', 'asdf') AS something_else;

The reason why I'm not inserting the data into an ordinary table rather than temp is because I want to use my db to calculate distances between geographical points (postgis), and I have a lot of data - doing this (plus sorting) would be too exhaustive for php and I don't need to store this data - I just want to make some calculations and return the set to my code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

看透却不说透 2024-10-25 04:18:28

您还可以按照 http 使用 VALUES 命令://www.postgresql.org/docs/9.0/interactive/sql-createtableas.html,所以

CREATE TEMP TABLE temp_tab (num integer, something_else char(4))
WITH (OIDS) ON COMMIT DROP AS 
VALUES (22, 'smth'), (23, 'wqer'), (24, 'asdf');

*注意:我还没有尝试过查询,所以那里可能有错字,但你应该得到这个想法。

You can also use a VALUES command as per http://www.postgresql.org/docs/9.0/interactive/sql-createtableas.html, so

CREATE TEMP TABLE temp_tab (num integer, something_else char(4))
WITH (OIDS) ON COMMIT DROP AS 
VALUES (22, 'smth'), (23, 'wqer'), (24, 'asdf');

*note: I haven't tried the query, so there may be a typo in there, but you should get the idea.

情绪 2024-10-25 04:18:28

我不使用 postgresql,但在 SQL Server 中,您可以使用 UNION ALL 构建多行文字。这对你有用吗?

CREATE TEMP TABLE temp_tab WITH (OIDS) ON COMMIT DROP AS
SELECT 22 AS num, 'smth' AS something_else union all
select 23, 'wqer' union all
select 24, 'asdf'

I don't use postgresql, but in SQL Server you can build up multiple rows of literals using UNION ALL. Does this work for you?

CREATE TEMP TABLE temp_tab WITH (OIDS) ON COMMIT DROP AS
SELECT 22 AS num, 'smth' AS something_else union all
select 23, 'wqer' union all
select 24, 'asdf'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文