2016-02-29 41 views
0

These are the tables that I have created for my Database.試圖完成複雜的查詢,與他們有麻煩

我想問問這幾個疑問,但他們不似乎工作不管我進入。我試圖找出如下:

我的第一個是:

select league, teamID from team, player, where Team.Player = Player.PlayerId
輸出是一條錯誤消息,207

什麼播放器具有最低的工資,還有什麼是他的地址,第一個和最後一個名稱? 什麼聯盟和地區是玩家#100玩的? 每個地區目前有多少玩家? 2014年以來球員#104參賽的球隊是什麼?

所有我寫了創建這個SQL的低於:

CREATE TABLE Team (
    TeamID INT NOT NULL, 
    Team VARCHAR(45) NOT NULL, 
    League VARCHAR(45) NOT NULL, 
    Region VARCHAR(45) NOT NULL, 
    PRIMARY KEY (TeamID)) 

INSERT INTO Team (TeamID, Team, League, Region) VALUES (12, 'Rays', 'American', 'East'); 
INSERT INTO Team (TeamID, Team, League, Region) VALUES (13, 'Giants', 'National', 'West'); 
INSERT INTO Team (TeamID, Team, League, Region) VALUES (14, 'Dodgers', 'National', 'West'); 
INSERT INTO Team (TeamID, Team, League, Region) VALUES (15, 'Blue Jays', 'American', 'East'); 
INSERT INTO Team (TeamID, Team, League, Region) VALUES (16, 'Royals', 'American', 'Central'); 

CREATE TABLE TEAMPLAYER (
    TeamID INT NOT NULL, 
    PlayerID VARCHAR(45) NOT NULL, 
    StartDate DATETIME NOT NULL, 
    EndDate DATETIME NULL, 
    PRIMARY KEY (TeamID, PlayerID)) 

INSERT INTO TEAMPLAYER (TeamID, PlayerID, StartDate, EndDate) VALUES (12, '100', '01/01/2013', NULL); 
INSERT INTO TEAMPLAYER (TeamID, PlayerID, StartDate, EndDate) VALUES (13, '101', '03/05/2014', NULL); 
INSERT INTO TEAMPLAYER (TeamID, PlayerID, StartDate, EndDate) VALUES (14, '102', '09/01/2013', NULL); 
INSERT INTO TEAMPLAYER (TeamID, PlayerID, StartDate, EndDate) VALUES (15, '103', '09/01/2013', '12/31/2013'); 
INSERT INTO TEAMPLAYER (TeamID, PlayerID, StartDate, EndDate) VALUES (16, '104', '06/23/2015', NULL); 
INSERT INTO TEAMPLAYER (TeamID, PlayerID, StartDate, EndDate) VALUES (16, '103', '01/01/2014', NULL); 
INSERT INTO TEAMPLAYER (TeamID, PlayerID, StartDate, EndDate) VALUES (12, '104', '03/05/2014', '06/22/2015'); 
INSERT INTO TEAMPLAYER (TeamID, PlayerID, StartDate, EndDate) VALUES (14, '103', '01/01/2008', '08/31/2013'); 
INSERT INTO TEAMPLAYER (TeamID, PlayerID, StartDate, EndDate) VALUES (15, '102', '05/05/2010', '08/31/2013'); 

CREATE TABLE Player (
    PlayerID INT NOT NULL, 
    PlayerLName VARCHAR(45) NOT NULL, 
    PlayerFName VARCHAR(45) NOT NULL, 
    HomeAddress VARCHAR(45) NULL, 
    Salary INT NOT NULL, 
    AgentID INT NOT NULL, 
    PRIMARY KEY (PlayerID)) 

INSERT INTO Player (PlayerID, PlayerLName, PlayerFName, HomeAddress, Salary, AgentID) VALUES (100, 'Jones', 'Larry', '1421 Rovetta Way', 500000, 35); 
INSERT INTO Player (PlayerID, PlayerLName, PlayerFName, HomeAddress, Salary, AgentID) VALUES (101, 'Armstrong', 'Sam', '221 Sovereign', 500000, 35); 
INSERT INTO Player (PlayerID, PlayerLName, PlayerFName, HomeAddress, Salary, AgentID) VALUES (102, 'Jones', 'David', '3333 Skip St', 300000, 34); 
INSERT INTO Player (PlayerID, PlayerLName, PlayerFName, HomeAddress, Salary, AgentID) VALUES (103, 'Ortiz', 'David', '1111 Jones Ave', 1400000, 36); 
INSERT INTO Player (PlayerID, PlayerLName, PlayerFName, HomeAddress, Salary, AgentID) VALUES (104, 'Davis', 'Chris', '1111 Jones Ave', 1100000, 38); 

CREATE TABLE Agent (
    AgentID INT NOT NULL, 
    AgentLName VARCHAR(45) NOT NULL, 
    AgentFName VARCHAR(45) NOT NULL, 
    AgentAddress VARCHAR(45) NOT NULL, 
    Phone BIGINT NOT NULL, 
    PRIMARY KEY (AgentID)) 

INSERT INTO Agent (AgentID, AgentLName, AgentFName, AgentAddress, Phone) VALUES (34, 'Brown', 'Harry', '100 Smart Way', 2135552222); 
INSERT INTO Agent (AgentID, AgentLName, AgentFName, AgentAddress, Phone) VALUES (35, 'Griffey', 'Ken', '150 Leaping Lizard', 2485332391); 
INSERT INTO Agent (AgentID, AgentLName, AgentFName, AgentAddress, Phone) VALUES (36, 'Brown', 'Norma', '175 Hartford', 5863435932); 
INSERT INTO Agent (AgentID, AgentLName, AgentFName, AgentAddress, Phone) VALUES (37, 'Armstrong', 'Deb', '1210 Clifton', 8505756157); 
INSERT INTO Agent (AgentID, AgentLName, AgentFName, AgentAddress, Phone) VALUES (38, 'Armstrong', 'Ken', '1210 Clifton', 8505756158); 
+0

提供一個您的查詢不起作用的例子,以及您獲得的錯誤。 –

+0

您可以向我們展示您迄今爲止撰寫的查詢,以及他們的輸出是什麼? –

+0

至少,標記正確的RDBMS,並在問題中提供數據表。開始[這裏](http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/) –

回答

1

你的第一選擇查詢有一個額外的逗號,但它採用了非常糟糕的編碼技術稱爲隱式連接。沒有任何情況下你應該使用這些。嘗試:

select league, teamID from team 
inner join player 
on Team.Player = Player.PlayerId 

如果您使用連接,所有其他查詢都非常簡單。如果你不知道該怎麼做,那就閱讀並徹底瞭解它們。如果不理解連接,就無法有效地進行查詢。