PostgreSQL 中有“AS”命令吗?

发布于 2024-11-30 20:24:20 字数 231 浏览 0 评论 0原文

我想知道 postgres 是否有像 AS 这样的命令。有谁知道postges有这个能力吗?我试图用谷歌搜索它,但对谷歌来说这是一个非常困难的问题:PI想要制作一个选择语句并将其存储为新的表名称。我想说的是:

SELECT subj, user AS 'new' FROM table_name;

I'm wondering if there is a command like AS for postgres. Does anyone know if postges has this ability? I've tried to google it but it's a very difficult question to google :P I want to make a select statement and store it as a new table name. I want to say something like:

SELECT subj, user AS 'new' FROM table_name;

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

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

发布评论

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

评论(7

笛声青案梦长安 2024-12-07 20:24:20

是的。它是 PostgreSQL 中保留的 SQL 关键字。请参阅链接文档页面上的表 C-1。

它通常与列标签。

AS 关键字是可选的,但前提是新列名不存在
匹配任何 PostgreSQL 关键字(参见附录 C)。

Yes. It's a reserved SQL key word in PostgreSQL. See Table C-1 at the linked documentation page.

It's typically used with column labels.

The AS keyword is optional, but only if the new column name does not
match any PostgreSQL keyword (see Appendix C).

饮惑 2024-12-07 20:24:20
CREATE TABLE new_table AS SELECT subj, user FROM table_name

这样,您的新桌子就准备好了。

CREATE TABLE new_table AS SELECT subj, user FROM table_name

So, your new table will be ready.

梦情居士 2024-12-07 20:24:20

“我想创建一条 select 语句并将其存储为新的表名”--

CREATE VIEW view_name AS
SELECT subj, user AS "new" FROM table_name;

"I want to make a select statement and store it as a new table name" --

CREATE VIEW view_name AS
SELECT subj, user AS "new" FROM table_name;
Oo萌小芽oO 2024-12-07 20:24:20

如果您不想在架构中留下任何更改,您可以执行以下操作 -

select new.* from (  
  select foo, bar from old  
) as new

If you don't want to leave any changes in the schema, you can do something like this-

select new.* from (  
  select foo, bar from old  
) as new
魄砕の薆 2024-12-07 20:24:20
  1. 开发您的查询:SELECT field AS new_field FROM table WHERE ...;
  2. 如果运行正常,请将其复制
  3. VIEW并执行以下操作:CREATE OR REPLACE VIEW new_view AS;
  4. 保存视图并将其用作表。
  5. 享受。
  1. develop your query: SELECT field AS new_field FROM table WHERE ...;
  2. If it runs ok, copy it
  3. Go to VIEW and do that: CREATE OR REPLACE VIEW new_view AS <QUERY>;
  4. Save the view and use it as table.
  5. enjoy.
子栖 2024-12-07 20:24:20

以我的方式,您可以为字段创建别名并创建视图来存储它。它就像桌子一样。

从 table_name 中选择新的主题、用户;
通过使用上面的查询可以获取 subj 和 new 作为字段。

并创建视图。

创建或替换 view_name 为
从 table_name 中选择新的主题、用户;

只需致电
选择视图名称;

In my way, you can do alias for the field and create view to store it. which ll act like table.

select subj, user as new from table_name;
By using the above query can get subj and new as a field.

And create view.

create or replace view_name as
select subj, user as new from table_name;

And just call
select view_name;

关于从前 2024-12-07 20:24:20

在 PostgreSQL 中,您可以使用或不使用 AS 创建列或表的别名。

例如,您创建 person 表,然后向其中插入 2 行,如下所示:

CREATE TABLE person (
  id INT,
  first_name VARCHAR(20),
  last_name VARCHAR(20)
);

INSERT INTO person (id, first_name, last_name) 
VALUES (1, 'John', 'Smith'), (2, 'David', 'Miller');

现在,您可以创建以下别名 f_nl_n first_namelast_name 列分别带或不带 AS 如下所示:

SELECT id, first_name AS f_n, last_name l_n FROM person;
             -- With `AS`   -- Without `AS`
SELECT person.id, person.first_name AS f_n, person.last_name l_n FROM person;
                           -- With `AS`          -- Without `AS`

然后,您可以得到如下结果:

 id |  f_n  |  l_n
----+-------+--------
  1 | John  | Smith
  2 | David | Miller
(2 rows

并且,您可以创建别名pperson 表的 f_nl_nfirst_namelast_name 列分别有或没有AS 如下所示:

SELECT id, f_n, l_n, p.id, p.f_n, p.l_n FROM person AS p(id, f_n, l_n);
                                           -- With `AS`
SELECT id, f_n, l_n, p.id, p.f_n, p.l_n FROM person p(id, f_n, l_n);
                                         -- Without `AS`

然后,可以得到如下结果:

 id |  f_n  |  l_n   | id |  f_n  |  l_n
----+-------+--------+----+-------+--------
  1 | John  | Smith  |  1 | John  | Smith
  2 | David | Miller |  2 | David | Miller
(2 rows)

In PostgreSQL, you can create the alias of a column or table with or without AS.

For example, you create person table, then insert 2 rows into it as shown below:

CREATE TABLE person (
  id INT,
  first_name VARCHAR(20),
  last_name VARCHAR(20)
);

INSERT INTO person (id, first_name, last_name) 
VALUES (1, 'John', 'Smith'), (2, 'David', 'Miller');

Now, you can create the aliases f_n and l_n of first_name and last_name column respectively with or without AS as shown below:

SELECT id, first_name AS f_n, last_name l_n FROM person;
             -- With `AS`   -- Without `AS`
SELECT person.id, person.first_name AS f_n, person.last_name l_n FROM person;
                           -- With `AS`          -- Without `AS`

Then, you can get the result below:

 id |  f_n  |  l_n
----+-------+--------
  1 | John  | Smith
  2 | David | Miller
(2 rows

And, you can create the aliases p, f_n and l_n of person table, first_name and last_name column respectively with or without AS as shown below:

SELECT id, f_n, l_n, p.id, p.f_n, p.l_n FROM person AS p(id, f_n, l_n);
                                           -- With `AS`
SELECT id, f_n, l_n, p.id, p.f_n, p.l_n FROM person p(id, f_n, l_n);
                                         -- Without `AS`

Then, you can get the result below:

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