【发布时间】:2015-04-23 19:55:05
【问题描述】:
我需要两张表,一张显示使用三个表的一对多关系,另一张显示多对多关系。我已经成功地完成了前 2 个查询,一个查询只涉及一个表,一个一对多涉及 2 个表。
单表查询:
SELECT LastName, FirstName, City
FROM Customer
WHERE City = 'Lutherville-Timonium'
一对多涉及 2 个表:
SELECT b.BrandName, m.ModelName
FROM Brand b, Model m
WHERE b.BrandID = m.BrandID
这是我的代码:
DROP DATABASE IF EXISTS FinalProject;
CREATE DATABASE IF NOT EXISTS FinalProject;
Use FinalProject;
CREATE TABLE IF NOT EXISTS Brand
(
BrandID INT,
BrandName VARCHAR(45),
PRIMARY KEY(BrandID)
);
CREATE TABLE IF NOT EXISTS Model
(
ModelName VARCHAR(45),
BrandID INT,
PRIMARY KEY(ModelName),
FOREIGN KEY(BrandID) REFERENCES Brand(BrandID)
);
CREATE TABLE IF NOT EXISTS Platform
(
PlatformID INT,
Platform VARCHAR(45),
PRIMARY KEY(PlatformID)
);
CREATE TABLE IF NOT EXISTS Computer
(
ComputerID INT,
ModelName VARCHAR(45),
PlatformID INT,
Processor VARCHAR(45),
Memory VARCHAR(45),
HardDrive VARCHAR(45),
OperatingSystem VARCHAR(45),
PRIMARY KEY(ComputerID),
FOREIGN KEY(ModelName) REFERENCES Model(ModelName),
FOREIGN KEY(PlatformID) REFERENCES Platform(PlatformID)
);
CREATE TABLE IF NOT EXISTS Customer
(
CustomerID INT,
LastName VARCHAR(45),
FirstName VARCHAR(45),
Address VARCHAR(45),
Zip INT,
City VARCHAR(45),
State VARCHAR(2),
PhoneNumber INT,
Email VARCHAR(45),
PRIMARY KEY(CustomerID)
);
CREATE TABLE IF NOT EXISTS Employee
(
EmployeeID INT,
Position VARCHAR(45),
LastName VARCHAR(45),
FirstName VARCHAR(45),
Address VARCHAR(45),
Zip INT,
City VARCHAR(45),
State VARCHAR(2),
PhoneNumber INT,
Email VARCHAR(45),
PRIMARY KEY(EmployeeID)
);
CREATE TABLE IF NOT EXISTS Invoice
(
InvoiceID INT,
CustomerID INT,
Total DECIMAL(19,4),
EmployeeID INT,
PRIMARY KEY(InvoiceID),
FOREIGN KEY(CustomerID) REFERENCES Customer(CustomerID),
FOREIGN KEY(EmployeeID) REFERENCES Employee(EmployeeID)
);
CREATE TABLE IF NOT EXISTS InvoiceItem
(
InvoiceItemID INT,
InvoiceID INT,
ComputerID INT,
PRIMARY KEY(InvoiceItemID),
FOREIGN KEY(InvoiceID) REFERENCES Invoice(InvoiceID),
FOREIGN KEY(ComputerID) REFERENCES Computer(ComputerID)
);
INSERT INTO Brand
(BrandID, BrandName)
VALUES
(101, 'Toshiba'),
(102, 'ASUS'),
(103, 'Dell'),
(104, 'Samsung'),
(105, 'MSI'),
(106, 'Apple');
INSERT INTO Model
(ModelName, BrandID)
VALUES
('Satellite L50D-BBT2N22', 101),
('Transformer Book T100TAF', 102),
('Inspiron 3000', 103),
('ATIV One 7 Curved', 104),
('GE60 2PE APACHE PRO', 105),
('Summer 2014 13 in. MacBook Pro', 106),
('Wind Box DC111', 105),
('Alienware 17', 103),
('ATIV Book 9 Plus', 104);
INSERT INTO Platform
(PlatformID, Platform)
VALUES
(001, 'Laptop'),
(002, 'Desktop');
INSERT INTO Computer
( ComputerID, ModelName, Processor, Memory, HardDrive, OperatingSystem, PlatformID)
VALUES
(178045, 'Satellite L50D-BBT2N22', 'AMD Quad-Core A4-6210', '4GB', '1TB', 'Windows 8.1', 001),
(178046, 'Transformer Book T100TAF', 'Intel Bay Trail-T Quad Core Z3735 1.33 GHz', '2GB', '32GB', 'Windows 8.1', 001),
(178047, 'Inspiron 3000', 'Intel Core i5-4460', '8GB', '1TB', 'Windows 8.1', 002),
(178048, 'ATIV One 7 Curved', 'Intel Core i5 Processor 5200U', '8GB', '1TB', 'Windows 8.1', 002),
(178049, 'GE60 2PE APACHE PRO', 'Intel Core i7 Processor', '16GB', '1TB', 'Windows 8.1', 001),
(178050, 'Summer 2014 13 in. MacBook Pro', '2.5GHz dual-core Intel Core i5', '4GB', '500GB', 'OS X Yosemite', 001),
(178051, 'Wind Box DC111', 'Intel Celeron Dual Core', '4GB', ' 500GB', 'Windows 8.1', 002),
(178052, 'Alienware 17', 'Intel Core i7', '8GB', '1TB', 'WIndows 8.1', 001),
(178053, 'ATIV Book 9 Plus', 'Intel Core i5', '8GB', '128GB', 'Windows 8.1', 001);
INSERT INTO Customer
(CustomerID, LastName, FirstName, Address, Zip, City, State, PhoneNumber, Email)
VALUES
(14670, 'Franks', 'Robert', '2905 North Ave.', 21218, 'Baltimore', 'MD', 443-875-9090, 'r.franks@gmail.com'),
(14671, 'Smith', 'Anthony', '28 Rhodes Pl.', 21093, 'Lutherville-Timonium', 'MD', 410-252-6542, 'asmith@me.com'),
(14672, 'Anderson', 'Mary', '1784 Cranbrook Dr.', 21093, 'Lutherville-Timonium', 'MD', 410-687-8235, 'm.anderson@comcast.net'),
(14673, 'Keith', 'Toby', '987 Rodeo Dr.', 21093, 'Lutherville-Timonium', 'MD', 443-267-0900, 'not_that_toby.keith@gmail.com'),
(14674, 'Karwacki', 'Ryan', '16200 Yeoho Rd', 21152, 'Hereford', 'MD', 410-350-4456, 'r.karwacki44@gmail.com'),
(14675, 'Yancey', 'Marcus', '165 Twilight Ct.', 21218, 'Baltimore', 'MD', 443-908-9087, 'm.yancey35@aol.com');
INSERT INTO Employee
(EmployeeID, Position, LastName, FirstName, Address, Zip, City, State, PhoneNumber, Email)
VALUES
(100001, 'CEO', 'Brocato', 'Christopher', '26 Rhodes Pl.', 21093, 'Lutherville-Timonium', 'MD', 410-812-0548, 'c.brocat0@prestigeww.com'),
(100893, 'Location Manager', 'White', 'Walter', '687 Winning Dr.', 21117, 'Owings Mills', 'MD', 410-674-8890, 'w.white@prestigeww.com'),
(100894, 'Computer Engineer', 'Pinkman', 'Jesse', '15 Pot Spring Cr.', 21093, 'Lutherville-Timonium', 'MD', 443-897-5467, 'j.pinkman@prestigeww.com'),
(100895, 'Computer Engineer', 'Fring', 'Gustavo', '8796 Westmister Br.', 21117, 'Owings Mills', 'MD', 443-098-1111, 'g.fring@prestigeww.com'),
(100896, 'Software Specialist', 'Boetticher', 'Gale', '7845 Sunny Ln.', 21093, 'Lutherville-Timonium', 'MD', 443-896-5674, 'g.boetticher@prestigeww.com'),
(100087, 'Lawyer', 'Goodman', 'Saul', '7823 Goodman Ln.', 21093, 'Lutherville-Timonium', 'MD', 410-657-8900, 's.goodman@prestigeww.com');
INSERT INTO Invoice
(InvoiceID, Total, CustomerID, EmployeeID)
VALUES
(237871, 115.99, 14670, 100893),
(237872, 87.89, 14671, 100894),
(237873, 476.95, 14672, 100895),
(237874, 314.95, 14673, 100896),
(237875, 45.96, 14674, 100001),
(237876, 79.84, 14675, 100087);
INSERT INTO InvoiceItem
(InvoiceItemID, InvoiceID, ComputerID)
VALUES
(1008, 237871, 178045),
(1009, 237872, 178046),
(1010, 237873, 178047),
(1011, 237874, 178048),
(1012, 237875, 178049),
(1013, 237876, 178050);
任何帮助将不胜感激 谢谢。
【问题讨论】:
-
您要连接哪些表?
-
我试图加入品牌、型号和计算机,但无法弄清楚
标签: mysql many-to-many relationship one-to-many