如何区分选择查询中两个表的相同字段名称?

发布于 2024-09-01 02:25:23 字数 714 浏览 3 评论 0原文

我的数据库中有两个以上的表,它们都包含相同的字段名称,就像

table A           table B       table C
field1            field1        field1
field2            field2        field2
field3            field3        field3
.                 .             .
.                 .             .
.                 .             .
.                 .             .

我必须编写一个 SELECT 查询一样,该查询从这 3 个表中获取几乎所有相同的字段。我正在使用类似这样的东西:

select a.field1,a.field2,a.field3,b.field1,b.field2,b.field3,c.field1,c.field2,c.field3 from table A as a, table B as b,table C as c where so and so.

但是当我打印 field1 的值时,它会给出最后一个表值。

如何获取具有相同字段名的三个表的所有值?我是否必须为每个表编写单独的查询,或者有什么方法可以在单个查询中获取所有表?

I have more than two tables in my database and all of them contains same field names like

table A           table B       table C
field1            field1        field1
field2            field2        field2
field3            field3        field3
.                 .             .
.                 .             .
.                 .             .
.                 .             .

I have to write a SELECT query which gets almost all same fields from these 3 tables. I am using something like this :

select a.field1,a.field2,a.field3,b.field1,b.field2,b.field3,c.field1,c.field2,c.field3 from table A as a, table B as b,table C as c where so and so.

but when I print field1's value it gives me the last table values.

How can I get all the values of three tables with the same field names? do I have to write individual query for every table OR there is any ways of fetching them all in a single query?

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

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

发布评论

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

评论(3

你丑哭了我 2024-09-08 02:25:23

就这样写吧

select a.field1 as af1,a.field2 as af2,a.field3 as af3,b.field1 as bf1,b.field2 as bf2,b.field3 as bf3,c.field1 as cf1,c.field2 as cf2,c.field3 as cf3 from table A as a, table B as b,table C as c where so and so.

Just write like this,

select a.field1 as af1,a.field2 as af2,a.field3 as af3,b.field1 as bf1,b.field2 as bf2,b.field3 as bf3,c.field1 as cf1,c.field2 as cf2,c.field3 as cf3 from table A as a, table B as b,table C as c where so and so.
伤痕我心 2024-09-08 02:25:23

这是编程工具处理重复字段名称的方式的产物。如果您愿意,可以使用 AS 为字段名称添加别名:

SELECT a.field1 AS a_field1, ...

然后它应该可以作为 a_field1 进行访问。

This is an artifact of how your programming tool handles duplicated field names. If you like, you can use AS to alias field names:

SELECT a.field1 AS a_field1, ...

It should then be accessible as a_field1.

萌酱 2024-09-08 02:25:23

您可以为列添加别名。例如注意:语法可能会根据您的数据库而有所不同。

SELECT
    a.field1 `A_Field1`,
    b.field1 `B_Field1`

SELECT
    a.field1 [A_Field1],
    b.field1 [B_Field1]

SELECT
    a.field1 AS A_Field1,
    b.field1 AS B_Field1

You can alias the columns. e.g. Note: The syntax can vary depending on your DB.

SELECT
    a.field1 `A_Field1`,
    b.field1 `B_Field1`

SELECT
    a.field1 [A_Field1],
    b.field1 [B_Field1]

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