[英]How to differentiate between same field names of two tables in a select query?
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.我必须编写一个
SELECT
查询,它从这 3 个表中获取几乎所有相同的字段。 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.但是当我打印
field1
的值时,它给了我最后一个表值。
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?
我是否必须为每个表编写单独的查询,或者有什么方法可以在单个查询中获取它们?
就这样写,
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.
This is an artifact of how your programming tool handles duplicated field names.这是您的编程工具如何处理重复字段名称的人工制品。 If you like, you can use
AS
to alias field names:如果您愿意,可以使用
AS
为字段名称设置别名:
SELECT a.field1 AS a_field1, ...
It should then be accessible as a_field1
.然后它应该可以作为
a_field1
访问。
You can alias the columns.您可以为列设置别名。 eg 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.