English 中文(简体)
卡斯特内尔加入两个表格,其名称同列。
原标题:SQL inner join two tables with the same column names
  • 时间:2010-07-29 17:49:25
  •  标签:
  • sql
  • sqlite

我有两张表格,各栏数各不相同。 (我不知道有多少栏目或名称是什么)例如表A和表B。

表A:

ID | B_ID | {variable} 

表B

ID | {variable} 

学历:

SELECT TableA.*, 表B.* FROM TableA INNER JOIN 表B ON TableA.B_ID= 表B.id;

When TableA and 表B both have a column with a same name I can t distinguish between the two different columns. For example of both tables has the column "Name" this query would result in :

ID | ID | B_ID | NAME | NAME |
 1 | 35 | 35   | bob  | jim  |

我所期待的是区分两个表格的方法。 最好有一栏名的前奏。

TableA_ID | 表B_ID | TableA_B_ID | TableA_NAME | 表B_NAME |
        1 |        35 |          35 |         bob |         jim |

I know of the "AS" keyword but the problem is that I don t know what the column names are going to be before hand. (I don t know if TableA or 表B are going to have the column Name)

因此,我的问题是:

当两个表的栏目可能有相同的栏目时,你如何区分两个表的栏目?

我正在使用Kallite3。

最佳回答

你的定本(问询)应当有所有表A栏,然后是所有表B表B的栏目,这样,你才能进入第二栏<代码>。 ID 缩略语

尽管如此,我觉得奇怪的是,你在两张表格中重新询问了所有数据,你在功能上不知道这些数据。

问题回答

这当然是一种 ha的解决办法,但:

SELECT TableA.*, "#", TableB.* 
FROM TableA INNER JOIN TableB ON TableA.B_ID= TableB.id;

编制结果清单,分两部分,左栏和右栏。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签