MySQL连接中的列名冲突

时间:2018-04-09 06:49:17

标签: mysql sql

我有两张名为王国和玩家的牌桌。

王国表:

+----------------------------------------
| ID | kingdom              | timestamp |
+----------------------------------------
| 1  | Kingdom 47           | time      |
| 2  | Kingdom 48           | time      |
----------------------------------------+

玩家表:

+---------------------------------------+
| ID | name                 | kingdom   |
+---------------------------------------+
| 7  | some name            | 1         |
| 8  | some name            | 1         |
| 9  | some name            | 1         |
+---------------------------------------+

我想加入他们并得到一些类似下面的内容:

我希望输出如下:

+-----------------------------------------+
| ID | name                 | kingdom     |
+-----------------------------------------+
| 7  | some name            | Kingdom 47  |
| 8  | some name            | Kingdom 47  |
| 9  | some name            | Kingdom 47  |
+-----------------------------------------+

我试过了:

 SELECT p.id, p.name, p.kingdom
 FROM player p, kingdom k
 WHERE k.id=p.kingdom and kingdom LIKE "%K47%";

我正在使用MySQL 5.7。

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT P.ID , P.name, K.kingdom
FROM player p 
INNER JOIN kingdom K ON K.ID = p.kingdom