创建具有相同列的视图

时间:2018-10-30 22:17:50

标签: sql view phpmyadmin

CREATE TABLE `player` (
  `id` int(11) NOT NULL,
  `id_team` int(11) NOT NULL,
  `name` varchar(40) NOT NULL,
  `img` text NOT NULL
)
CREATE TABLE `matchs` (
  `id` int(11) NOT NULL,
  `date` datetime NOT NULL,
  `player_1` int(11) NOT NULL,
  `player_2` int(11) NOT NULL
)
CREATE TABLE `score` (
  `id_matchs` int(11) NOT NULL,
  `id_player` int(11) NOT NULL,
  `score` int(11) NOT NULL
)

我正在尝试创建一个视图以获取如下内容:

|img_p1|name_p1|score_p1|score_p2|name_p2|img_p1|
+------+-------+--------+--------+-------+------+
|      |       |        |        |       |      |
|      |       |        |        |       |      |

但是我真的不知道从哪里开始,因为img,名称和得分有2个相同的列。

编辑:

因此,借助mdem7,我能够解决此问题。这是代码:

SELECT p1.img, p1.name, s1.score, s2.score, p2.nom, p2.img
FROM player p1, player p2, matches m, score s1, score s2
WHERE p1.id = s1.id_player AND m.id = s1.id_matchs AND p2.id = s2.id_player
AND m.id = s2.id_matchs AND m.player_1 = p1.id AND m.player_2 = j2.id;

1 个答案:

答案 0 :(得分:1)

就像您的示例一样,您需要alias列名

以下不是完整的SQL,因为给出的信息有限,但是 您可以使用它来创建自己的游戏。

select p1.img as img_p1, p1.name as name_p1, s1.score as score_p1,
       s2.score as score_p2, p2.name as name_p2, p2.img as img_p2
from player p1, player p2, matches m1, matches m2, scores s1, scores s2
where p1.id = s1.id_player and m1.id = s1.id_matches
      p2.id = s2.id_player and m2.id = s2.id_matches
...