尝试完成复杂的查询,遇到问题

时间:2016-02-29 20:10:40

标签: sql sql-server

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);

1 个答案:

答案 0 :(得分:1)

您的第一个选择查询有一个额外的逗号,但它使用了一种非常差的编码技术,称为隐式连接。没有任何情况下你应该使用它们。尝试:

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

如果使用连接,所有其他查询都非常简单。如果您不知道该怎么做,那么请仔细阅读并彻底了解它们。您无法在不了解联接的情况下进行有效查询。