0

如何使用一个 SQL 查询连接 2 个具有不同列名的表?

假设我有这张桌子:

+----+--------+----------+
| id |  name  |   car    |
+----+--------+----------+
|  1 | Name 1 | Audi     |
|  2 | Name 2 | BMW      |
|  3 | Name 3 | Mercedes |
+----+--------+----------+

还有这张表

+----+--------+----------+
| id |  name  |   bike   |
+----+--------+----------+
|  1 | Name 4 | Kawasaki |
|  2 | Name 5 | Triumph  |
|  3 | Name 6 | Ducati   |
+----+--------+----------+

我想把它变成这样

+--------+----------+
|  name  |  Brand   |
+--------+----------+
| Name 1 | Audi     |
| Name 2 | BMW      |
| Name 3 | Mercedes |
| Name 4 | Kawasaki |
| Name 5 | Triumph  |
| Name 6 | Ducati   |
+--------+----------+
4

3 回答 3

3

这看起来像你想要一个工会

SELECT name, car as brand
FROM cars
UNION ALL
SELECT name, bike as brand
FROM bikes
于 2013-06-10T20:46:57.283 回答
0
SELECT table1.name, table1.car AS Brand FROM table1 UNION ALL SELECT table2.name, table2.bike AS Brand FROM table2
于 2013-06-10T20:47:21.160 回答
0
select Name, Car from Table1
Union All
Select Name, Bike from Table2

我不知道您列出的 table1 和 2 的名称,但我猜它们与 Car and Bike 有关。

于 2013-06-10T20:48:17.190 回答