返回: 元组列表,每个元组包含(id,name,wins,matches): id:播放器的唯一ID(由数据库分配) name:玩家的全名(已注册) 胜利:玩家赢得的比赛数量 匹配:玩家所玩的匹配数量
我的sql脚本如下:
--if the DB already exists drop it so i can create it again
DROP DATABASE IF EXISTS tournament;
--create DB and connect to it
CREATE DATABASE tournament;
\c tournament;
--create table for players
CREATE TABLE players (
id SERIAL,
name TEXT,
PRIMARY KEY (id)
);
--create table for matches
CREATE TABLE matches (
id_match SERIAL,
id_winner INTEGER NULL REFERENCES players(id),
id_looser INTEGER NULL REFERENCES players(id),
PRIMARY KEY (id_match)
);
--create the view to count all the wins of a player
CREATE VIEW countWins AS
SELECT players.id AS ID,players.name AS Name,coalesce(count(matches.id_winner),0) as Record
FROM players, matches WHERE players.id = matches.id_winner
GROUP BY players.id;
--create the view for all the matches a players has played
CREATE VIEW countMatches AS
SELECT players.id AS ID,players.name AS Name,count(*) AS Played
FROM players, matches WHERE players.id = matches.id_winner OR players.id = matches.id_looser
GROUP BY players.id;
--
CREATE VIEW standings AS
SELECT countMatches.ID,countMatches.Name,coalesce(countWins.Record,0) AS wins,countMatches.Played
FROM countMatches LEFT JOIN countWins
ON countWins.ID = countMatches.ID or wins = 0
ORDER BY wins DESC;
当我运行
时\ i tournament.sql
在命令行中我收到此错误:
DROP DATABASE
CREATE DATABASE
You are now connected to database "tournament" as user "vagrant".
CREATE TABLE
CREATE TABLE
CREATE VIEW
CREATE VIEW
psql:tournament.sql:42: ERROR: column "wins" does not exist
LINE 4: ON countWins.ID = countMatches.ID or wins = 0
我不明白为什么如果我在#34;排名"视图...