我的疑问有什么问题?

时间:2013-05-09 13:38:58

标签: sql join where

所有代码都是正确的,但查询(位于最底层)似乎没有运行。我查看了w3schools.com以获取更多信息,但似乎我的代码没有任何问题。

有人可以看看并解释一下吗?

--Drop tables-- 
DROP TABLE Job; 
DROP TABLE Employee; 
DROP TABLE Purchase; 
DROP TABLE SoccerBall; 
DROP TABLE Client; 

--Create tables-- 
CREATE TABLE Client 
    ( 
    ClientId INT NOT NULL, 
    ClientName VARCHAR(20) NOT NULL, 
    Street VARCHAR(20) NOT NULL, 
    City VARCHAR(20) NOT NULL, 
    ZipCode VARCHAR(5) NOT NULL, 
    Phone VARCHAR(15) NULL, 
    EmailAddr VARCHAR(50) NULL, 
    PRIMARY KEY(ClientId) 
    ); 

CREATE TABLE SoccerBall 
    ( 
    BallId INT NOT NULL, 
    BallSize NUMBER(1) NOT NULL, 
    Color VARCHAR(20) NOT NULL, 
    Material VARCHAR(20) NOT NULL, 
    PRIMARY KEY(BallId) 
    ); 

CREATE TABLE Purchase 
    ( 
    PurchaseId INT NOT NULL, 
    ClientId INT NOT NULL, 
    BallId INT NOT NULL, 
    PurchaseDate DATE NOT NULL, 
    PRIMARY KEY (PurchaseId), 
    FOREIGN KEY (ClientId) REFERENCES Client (ClientId), 
    FOREIGN KEY (BallId) REFERENCES SoccerBall (BallId) 
    ); 

CREATE TABLE Employee 
    ( 
    EmployeeId INT NOT NULL, 
    EmployeeName VARCHAR(50) NOT NULL, 
    PRIMARY KEY (EmployeeId) 
    ); 

CREATE TABLE Job 
    ( 
    JobId INT NOT NULL, 
    PurchaseId INT NOT NULL, 
    EmployeeId INT NOT NULL, 
    AssignDate DATE NOT NULL, 
    PRIMARY KEY (JobId), 
    FOREIGN KEY (EmployeeId) REFERENCES Employee (EmployeeId) 
    ); 

--Insert data-- 
INSERT INTO Client VALUES 
    (1, 'Adidas', '123 Adidas Lane', 'Adida', '22209', '1-800-555-1987', 'corporate@adidas.com'); 
INSERT INTO Client VALUES
    (2, 'Nike', '234 Nike Drive', 'Nikail', '22182', '1-888-555-9876', 'corporate@nike.com'); 
INSERT INTO Client VALUES
    (3, 'Puma', '345 Puma Blvd', 'Puma City', '20912', '1-800-555-6247', 'corporate@puma.com'); 
INSERT INTO Client VALUES
    (4, 'Reebok', '456 Reebok Cove', 'Old Reebok', '20902', '1-800-555-1000', 'corporate@reebok.com'); 
INSERT INTO Client VALUES
    (5, 'Vans', '567 Vans Lane', 'Vancouver', '20725', '1-900-666-2000', 'corporate@vans.com'); 
INSERT INTO Client VALUES
    (6, 'Jordan', '234 Nike Drive', 'Nikail', '22182', '1-888-555-8765', 'corporate@jordan.com'); 
INSERT INTO Client VALUES
    (7, 'Diadora', '345 Puma Blvd', 'Puma City', '20912', '1-800-555-4126', 'corporate@diadora.com'); 




INSERT INTO SoccerBall VALUES
    (11, 1, 'blue', 'felt'); 
INSERT INTO SoccerBall VALUES
    (12, 3, 'red', 'leather'); 
INSERT INTO SoccerBall VALUES
    (13, 3, 'yellow', 'faux leather'); 
INSERT INTO SoccerBall VALUES
    (14, 5, 'black', 'leather'); 
INSERT INTO SoccerBall VALUES
    (15, 4, 'white', 'faux leather'); 
INSERT INTO SoccerBall VALUES
    (16, 3, 'purple', 'rubber'); 
INSERT INTO SoccerBall VALUES
    (17, 1, 'blue', 'faux leather'); 




INSERT INTO Purchase VALUES
    (21, 2, 11, DATE '2013-01-01'); 
INSERT INTO Purchase VALUES
    (22, 4, 12, DATE '2012-03-24'); 
INSERT INTO Purchase VALUES
    (23, 7, 15, DATE '2013-05-01'); 
INSERT INTO Purchase VALUES
    (24, 5, 13, DATE '2005-12-30'); 
INSERT INTO Purchase VALUES
    (25, 6, 16, DATE '1999-01-23'); 




INSERT INTO Employee VALUES
    (111, 'Vivin Viswanathan'); 
INSERT INTO Employee VALUES
    (222, 'Andy Edwards'); 
INSERT INTO Employee VALUES
    (333, 'Frank Hellwig'); 
INSERT INTO Employee VALUES
    (444, 'Vandana Janeja'); 




INSERT INTO Job VALUES
    (1111, 21, 111, DATE '2013-01-02'); 
INSERT INTO Job VALUES
    (5555, 22, 111, DATE '2012-03-26'); 
INSERT INTO Job VALUES
    (2222, 23, 222, DATE '2013-05-08'); 
INSERT INTO Job VALUES
    (3333, 24, 222, DATE '2006-01-02'); 
INSERT INTO Job VALUES
    (4444, 25, 111, DATE '1999-01-30'); 




--Queries-- 
--Query 1--Show me the information in the database 
SELECT * FROM Client; 
SELECT * FROM SoccerBall; 
SELECT * FROM Purchase; 
SELECT * FROM Employee; 
SELECT * FROM Job; 




--"Forgot" Quantity Attribute. Adding that-- 
ALTER TABLE Purchase ADD Quantity NUMBER(5); 

UPDATE Purchase SET Quantity = '12000' WHERE ClientId = '1'; 
UPDATE Purchase SET Quantity = '2492' WHERE ClientId = '2'; 
UPDATE Purchase SET Quantity = '94203' WHERE ClientId = '3'; 
UPDATE Purchase SET Quantity = '4394' WHERE ClientId = '4'; 
UPDATE Purchase SET Quantity = '0' WHERE ClientId = '5'; 
UPDATE Purchase SET Quantity = '4832' WHERE ClientId = '6'; 
UPDATE Purchase SET Quantity = '10002' WHERE ClientId = '7'; 

SELECT * FROM Purchase; 




--Queries Continued-- 
--Query 2--I want to know how many clients we have 
SELECT COUNT(*) FROM Client; 

--Query 3--Which client ordered the most soccer balls?*********** 
SELECT MAX(Quantity), ClientName 
FROM Purchase P, Client C 
WHERE P.ClientId = C.ClientId; 

--Query 4--How many projects does each employe have?*********** 
SELECT EmployeeId COUNT(*) 
FROM Job 
GROUP BY EmployeeId; 

--Query 5--First purchases, first served. Show me the order of the purchases by date*********** 
SELECT ClientName, PurchaseDate 
FROM Purchase P, Client C 
WHERE P.ClientId = C.ClientId AND ORDER BY PurchaseDate; 

--Query 6--Show me what kind of balls were purchased*********** 
SELECT B.BallId, B.BallSize, B.Color, B.Material 
FROM SoccerBall B, Purchase P, Client C 
WHERE P.ClientId = C.ClientId AND P.BallId = B.BallId AND C.ClientId = 1;    

--Query 7--Show me who purchased what balls************** 
SELECT B.BallId, B.BallSize, B.Color, B.Material, C.ClientId, ClientName 
FROM SoccerBall B, Purchase P, Client C 
WHERE P.ClientId = C.ClientId AND P.BallId = B.BallId GROUP BY ClientName;

非常感谢!

2 个答案:

答案 0 :(得分:3)

您的查询存在多个问题。

查询3缺少具有聚合函数的GROUP BY

SELECT MAX(Quantity), ClientName 
FROM Purchase P
INNER JOIN Client C 
   ON P.ClientId = C.ClientId
GROUP BY ClientName; 

查询4缺少列employeeIdcount(*)之间的逗号:

SELECT EmployeeId, COUNT(*) 
FROM Job 
GROUP BY EmployeeId; 

查询5在AND

之前有错误ORDER BY
SELECT ClientName, PurchaseDate 
FROM Purchase P
INNER JOIN Client C 
  ON P.ClientId = C.ClientId 
ORDER BY PurchaseDate; 

查询6您是否想要购买所有球,我认为您不希望在ClientId上使用过滤器:

SELECT B.BallId, B.BallSize, B.Color, B.Material 
FROM SoccerBall B
INNER JOIN Purchase P
  ON P.BallId = B.BallId
INNER JOIN Client C 
  ON P.ClientId = C.ClientId;    

查询7仅在一列上使用GROUP BY,但无法正常工作:

SELECT B.BallId, B.BallSize, B.Color, B.Material, C.ClientId, ClientName 
FROM SoccerBall B
INNER JOIN Purchase P
  ON P.BallId = B.BallId
INNER JOIN Client C 
  ON P.ClientId = C.ClientId;

请参阅SQL Fiddle with Demo

作为旁注,您会注意到我更新了查询以使用INNER JOIN语法而不是表和WHERE子句中的连接之间的逗号。

答案 1 :(得分:0)

这个有效。尝试一下。

 ' --Drop tables--
DROP TABLE job;

DROP TABLE employee;

DROP TABLE purchase;

DROP TABLE soccerball;

DROP TABLE client;

--Create tables--
CREATE TABLE client
  (
     clientid   INT NOT NULL IDENTITY(1, 1),
     clientname VARCHAR(20) NOT NULL,
     street     VARCHAR(20) NOT NULL,
     city       VARCHAR(20) NOT NULL,
     zipcode    VARCHAR(5) NOT NULL,
     phone      VARCHAR(15) NULL,
     emailaddr  VARCHAR(50) NULL,
     PRIMARY KEY(clientid)
  );

CREATE TABLE soccerball
  (
     ballid   INT NOT NULL,
     ballsize NUMERIC(1) NOT NULL,
     color    VARCHAR(20) NOT NULL,
     material VARCHAR(20) NOT NULL,
     PRIMARY KEY(ballid)
  );

CREATE TABLE purchase
  (
     purchaseid   INT NOT NULL,
     clientid     INT NOT NULL,
     ballid       INT NOT NULL,
     purchasedate DATETIME NOT NULL,
     PRIMARY KEY (purchaseid),
     FOREIGN KEY (clientid) REFERENCES client (clientid),
     FOREIGN KEY (ballid) REFERENCES soccerball (ballid)
  );

CREATE TABLE employee
  (
     employeeid   INT NOT NULL,
     employeename VARCHAR(50) NOT NULL,
     PRIMARY KEY (employeeid)
  );

CREATE TABLE job
  (
     jobid      INT NOT NULL,
     purchaseid INT NOT NULL,
     employeeid INT NOT NULL,
     assigndate DATETIME NOT NULL,
     PRIMARY KEY (jobid),
     FOREIGN KEY (employeeid) REFERENCES employee (employeeid)
  )

--Insert data--
INSERT INTO dbo.client
            (clientname,
             street,
             city,
             zipcode,
             phone,
             emailaddr)
VALUES      ( 'Adidas',
              '123 Adidas Lane',
              'Adida',
              '22209',
              '1-800-555-1987',
              'corporate@adidas.com')

INSERT INTO client
            (clientname,
             street,
             city,
             zipcode,
             phone,
             emailaddr)
VALUES      ('Nike',
             '234 Nike Drive',
             'Nikail',
             '22182',
             '1-888-555-9876',
             'corporate@nike.com')

INSERT INTO client
VALUES      ( 'Puma',
              '345 Puma Blvd',
              'Puma City',
              '20912',
              '1-800-555-6247',
              'corporate@puma.com')

INSERT INTO client
VALUES      ( 'Reebok',
              '456 Reebok Cove',
              'Old Reebok',
              '20902',
              '1-800-555-1000',
              'corporate@reebok.com')

INSERT INTO client
VALUES      ( 'Vans',
              '567 Vans Lane',
              'Vancouver',
              '20725',
              '1-900-666-2000',
              'corporate@vans.com')

INSERT INTO client
VALUES      ( 'Jordan',
              '234 Nike Drive',
              'Nikail',
              '22182',
              '1-888-555-8765',
              'corporate@jordan.com')

INSERT INTO client
VALUES      ( 'Diadora',
              '345 Puma Blvd',
              'Puma City',
              '20912',
              '1-800-555-4126',
              'corporate@diadora.com')

INSERT INTO soccerball
VALUES      (11,
             1,
             'blue',
             'felt');

INSERT INTO soccerball
VALUES      (12,
             3,
             'red',
             'leather');

INSERT INTO soccerball
VALUES      (13,
             3,
             'yellow',
             'faux leather');

INSERT INTO soccerball
VALUES      (14,
             5,
             'black',
             'leather');

INSERT INTO soccerball
VALUES      (15,
             4,
             'white',
             'faux leather');

INSERT INTO soccerball
VALUES      (16,
             3,
             'purple',
             'rubber');

INSERT INTO soccerball
VALUES      (17,
             1,
             'blue',
             'faux leather');

INSERT INTO purchase
VALUES      (21,
             2,
             11,
             '2013-01-01');

INSERT INTO purchase
VALUES      (22,
             4,
             12,
             '2012-03-24');

INSERT INTO purchase
VALUES      (23,
             7,
             15,
             '2013-05-01');

INSERT INTO purchase
VALUES      (24,
             5,
             13,
             '2005-12-30');

INSERT INTO purchase
VALUES      (25,
             6,
             16,
             '1999-01-23');

INSERT INTO employee
VALUES      (111,
             'Vivin Viswanathan');

INSERT INTO employee
VALUES      (222,
             'Andy Edwards');

INSERT INTO employee
VALUES      (333,
             'Frank Hellwig');

INSERT INTO employee
VALUES      (444,
             'Vandana Janeja');

INSERT INTO job
VALUES      (1111,
             21,
             111,
             '2013-01-02');

INSERT INTO job
VALUES      (5555,
             22,
             111,
             '2012-03-26');

INSERT INTO job
VALUES      (2222,
             23,
             222,
             '2013-05-08');

INSERT INTO job
VALUES      (3333,
             24,
             222,
             '2006-01-02');

INSERT INTO job
VALUES      (4444,
             25,
             111,
             '1999-01-30');

--Queries--
--Query 1--Show me the information in the database
SELECT *
FROM   client;

SELECT *
FROM   soccerball;

SELECT *
FROM   purchase;

SELECT *
FROM   employee;

SELECT *
FROM   job;

--"Forgot" Quantity Attribute. Adding that--
ALTER TABLE purchase
  ADD quantity NUMERIC;

UPDATE purchase
SET    quantity = '12000'
WHERE  clientid = '1';

UPDATE purchase
SET    quantity = '2492'
WHERE  clientid = '2';

UPDATE purchase
SET    quantity = '94203'
WHERE  clientid = '3';

UPDATE purchase
SET    quantity = '4394'
WHERE  clientid = '4';

UPDATE purchase
SET    quantity = '0'
WHERE  clientid = '5';

UPDATE purchase
SET    quantity = '4832'
WHERE  clientid = '6';

UPDATE purchase
SET    quantity = '10002'
WHERE  clientid = '7';

SELECT *
FROM   purchase;

--Queries Continued--
--Query 2--I want to know how many clients we have
SELECT Count(*)
FROM   client;

--Query 3--Which client ordered the most soccer balls?***********
SELECT Max(quantity),
       clientname
FROM   purchase P,
       client C
WHERE  P.clientid = C.clientid
GROUP  BY clientname

--Query 4--How many projects does each employe have?***********
SELECT Count(*)
FROM   job

--Query 5--First purchases, first served. Show me the order of the purchases by date***********
SELECT clientname,
       purchasedate
FROM   purchase P,
       client C
WHERE  P.clientid = C.clientid
ORDER  BY purchasedate;

--Query 6--Show me what kind of balls were purchased***********
SELECT B.ballid,
       B.ballsize,
       B.color,
       B.material
FROM   soccerball B,
       purchase P,
       client C
WHERE  P.clientid = C.clientid
       AND P.ballid = B.ballid
       AND C.clientid = 1;

--Query 7--Show me who purchased what balls**************
SELECT B.ballid,
       B.ballsize,
       B.color,
       B.material,
       C.clientid,
       clientname
FROM   soccerball B,
       purchase P,
       client C
WHERE  P.clientid = C.clientid
       AND P.ballid = B.ballid
GROUP  BY B.ballid,
          B.ballsize,
          B.color,
          B.material,
          C.clientid,
          clientname'