加入多个表中的列

时间:2012-11-01 20:26:51

标签: mysql database join inner-join

以下是我需要帮助的数据库的一部分。

DROP DATABASE IF EXISTS bookex;
CREATE DATABASE bookex;
use Bookex;


CREATE TABLE Reviewer(
   ReviewerID INT,
   Name VARCHAR(20) NOT NULL,
   EmployedBy CHAR(30),
   constraint reviewer_reviewerID_pk PRIMARY KEY (reviewerID)
);

CREATE TABLE Book(
   ISBN        CHAR(13),
   Title       VARCHAR(70) NOT NULL,
   Description VARCHAR(100),
   Category    INT,
   Edition     CHAR(30),
   PublisherID INT         NOT NULL,
   constraint book_ISBN_pk PRIMARY KEY (ISBN)
);

CREATE TABLE BookReview(
   ISBN CHAR(13) NOT NULL,
   ReviewerID INT NOT NULL,
   Rating INT,
   Comments VARCHAR(255),
   constraint bookreview_pk PRIMARY KEY (ISBN, ReviewerID),
   constraint bookreview_ISBN_fk FOREIGN KEY (ISBN) REFERENCES Book(ISBN),
   constraint bookreview_reviewerID_fk FOREIGN KEY (ReviewerID) REFERENCES Reviewer(ReviewerID)
);


--
-- Dumping data for table 'reviewer'
--

INSERT INTO reviewer VALUES (1,'Jacobs','Gadget Boy');
INSERT INTO reviewer VALUES (2,'Troell','Dallas Underground');
INSERT INTO reviewer VALUES (3,'Bills','The RIT Reporter');
INSERT INTO reviewer VALUES (4,'Hill','Microsoft News');
INSERT INTO reviewer VALUES (5,'Kurtz','MacIntosh News');
INSERT INTO reviewer VALUES (6,'Leone',null);
INSERT INTO reviewer VALUES (7,'Doubleday','Media Mania');
INSERT INTO reviewer VALUES (8,'Wells','The RIT Reporter');
INSERT INTO reviewer VALUES (9,'Worthington','The Annex');
INSERT INTO reviewer VALUES (10,'Lawson','The Last Word');
INSERT INTO reviewer VALUES (11,'Lasky',null);
INSERT INTO reviewer VALUES (12,'Rozanski',null);
INSERT INTO reviewer VALUES (13,'Whittington','JAVA Rocks');
INSERT INTO reviewer VALUES (14,'Holden',null);
INSERT INTO reviewer VALUES (15,'Stratton','Harvard Lampoon');
INSERT INTO reviewer VALUES (16,'Johnson','NRA Weekly');
INSERT INTO reviewer VALUES (17,'Mason','BMW Owners Quarterly');
INSERT INTO reviewer VALUES (18,'Goodman','MacIntosh News');
INSERT INTO reviewer VALUES (19,'Phelps','Detroit News');
INSERT INTO reviewer VALUES (20,'Yacci','The Learning Channel');
INSERT INTO reviewer VALUES (24,'Weeden','Scrye');
INSERT INTO reviewer VALUES (25,'McVea','Programming Central');
INSERT INTO reviewer VALUES (26,'Perez-Hardy','The Network Voice');
INSERT INTO reviewer VALUES (27,'Heartthrob','Journal of Romance');
INSERT INTO reviewer VALUES (28,'Bierre','JAVA Rocks');
INSERT INTO reviewer VALUES (29,'Axelrod','JAVA Rocks');


-- Dumping data for table 'book'
--

INSERT INTO book VALUES ('1-111-11111-4','Women are From Venus ORACLE is from Beyond Pluto','New York Times Best Seller 27 weeks',1,'4',1);
INSERT INTO book VALUES ('1-222-32443-7','Calculus for Phys Ed Majors','Carry our 200 lb text book: learn integrals and keep in Shape',7,'3',7);
INSERT INTO book VALUES ('1-295-84547-1','My Love\'s Last Longing','Volume 1 of 4',1,'1',4);
INSERT INTO book VALUES ('1-533-73363-8','From the Shores of Lake Erie to IT','One man\'s Journey to IT',8,'2',1);
INSERT INTO book VALUES ('1-559-68777-1','My Love\'s Last Lingering Lost','Volume 2 of 4',1,'1',4);
INSERT INTO book VALUES ('1-932-87447-6','The Science of Literature Searching','A Book Worth a Look is Worth a Good Look Up',6,'1',5);
INSERT INTO book VALUES ('2-089-77869-5','Yes! Networking is for Bills Fans','How to Use a Spanning Tree to Exit Rich Stadium',9,'1',8);
INSERT INTO book VALUES ('2-122-29058-5','How to Keep your Cellular Bill Down','How to carry 500 Miles of Fiber in Your Wallet',5,'2',2);
INSERT INTO book VALUES ('2-221-11341-5','Tired of wired? Infrared instead!','500 Ways to pull out your hair while pulling out all your cable',5,'1',2);
INSERT INTO book VALUES ('3-444-95595-4','ER, SOM, NF, DK/NF, SQL, JDBC, ODBC, and RELVAR','How U can avoid being trapped in Database acronyms',4,'1',3);
INSERT INTO book VALUES ('3-445-43341-2','Master Wireless Through The Classic Comics','Infrared man triumphs over Cable Guy and CoAx NastyMan',2,'1',6);
INSERT INTO book VALUES ('3-445-58668-5','How to Keep your Cable Bill Down','How to Avoid Tripping on 500 Miles of Fiber in your wallet',5,'1',10);
INSERT INTO book VALUES ('4-344-22221-1','I Lasted my Love\'s Last Lingering Longing','Volume 3 of 4',1,'2',4);
INSERT INTO book VALUES ('4-668-39283-8','From Deep in the Heart of Texas to IT','One man\'s Journey to IT',8,'1',1);
INSERT INTO book VALUES ('5-538-55746-1','My Lost Love\'s Long Last Lingering','Volume 4 of 4',1,'1',4);
INSERT INTO book VALUES ('6-449-68668-8','From Brockport to IT','Prof. Johnson\'s Journey to IT (about 25 min via 490)',8,'1',1);
INSERT INTO book VALUES ('7-999-14432-2','Master HTML Through The Classic Comics','Using his Super Web thrower, Spiderman creates HTML docs',2,'2',6);
INSERT INTO book VALUES ('8-330-69586-6','The Shortest Book in the World','A Guide to how Detroit will win the World Series in 2001',9,'1',3);
INSERT INTO book VALUES ('9-333-44441-1','My Love\'s at Long Last Lost his Lingering','Volume 5 of 4',1,'1',4);
INSERT INTO book VALUES ('9-337-55467-3','How to add Class to your Programming','How to write programs in VB instead of C',3,'3',10);
INSERT INTO book VALUES ('9-400-55555-2','Master C++ Through The Classic Comics','GeekMan triumphs over C++ program infected with Kryptonite virus',2,'1',6);
INSERT INTO book VALUES ('9-444-43356-3','A language without Pointers? Priceless.','For Everything Else, there\'s C++',3,'1',13);
INSERT INTO book VALUES ('9-455-61223-3','JAVA: It\'s more than Just a Programming Language','Achieve enlightenment through the mystery of the Swing package',3,'1',7);


INSERT INTO bookreview VALUES ('1-111-11111-4',19,10,'Hold on to your hats, gang! If you think a cookbook or the yellow pages is exciting reading, you gotta read Professor Spadas remarkable treatise on enriching you personal relationships through your knowledge of ORACLE 8.1.6. In an easy-to-read fashion, s');
INSERT INTO bookreview VALUES ('1-295-84547-1',27,10,'This is one of the most outstanding books ever written. Five thumbs up. Five stars. A must-read for everyone.');
INSERT INTO bookreview VALUES ('1-533-73363-8',20,4,'A rather boring autobiography of a northern Ohio native who migrates to Rochester New York where he becomes a professor of Information Technology at RIT. Other than the chapters devoted to the Cleveland Indians (chapters 1, 2, 3, 6, 10, 11, 12, 13), The C');
INSERT INTO bookreview VALUES ('1-559-68777-1',2,8,'A wonderful romance novel about Matt, a successful editor for the New York Times, who resigns and retreats to a Dude Ranch in New Mexico to find himself.  Matt meets Margaret, a person with cowgirl charm in touch with her masculine self. Matt falls in lov');
INSERT INTO bookreview VALUES ('1-559-68777-1',19,9,'A wonderful romance novel about Margaret, a successful editor for the New York Times, who resigns and retreats to a Dude Ranch in New Mexico to find herself.  Margaret meets Matt, a person with tough cowboy swagger in touch with his feminine self. Margare');
INSERT INTO bookreview VALUES ('2-089-77869-5',5,5,'Definitely dont buy this book if you are a Giants fan.  Two Giants fans tried this technique at the Giants game several weeks ago and they arent back yet.');
INSERT INTO bookreview VALUES ('2-089-77869-5',13,8,'For any networking buff/Bills fan, Dr. Lutz treatise on the use of spanning trees to exit a Buffalo Bills game is a must. It is difficult to slog through in spots, but he shows you that by applying his minimalization technique you can get back to Rochest');
INSERT INTO bookreview VALUES ('2-089-77869-5',15,6,'Dont buy this book unless your are  Bills fan.  Fans of any other team will find their departure time from a Bills game actually triples when the technique is applied. In fact, it probably accounts for why Dr. Lutz (and any other Bills fan who applies it');
INSERT INTO bookreview VALUES ('2-122-29058-5',6,7,'Professor Hartpence shows you how to carry 500 miles of fiber in your wallet.  Warning to the unwary: dont buy this book unless you have deep pockets.');
INSERT INTO bookreview VALUES ('2-122-29058-5',13,8,'Watch out Jim Carey .. this non-cable guy may leave you out of work. In his book, Professor Hartpence combines his cellular and networking skills with his experience in both the military and the commercial world and the practice of Zen to show you how to');
INSERT INTO bookreview VALUES ('3-444-95595-4',3,4,'U lose if you ever bother to read this book.');
INSERT INTO bookreview VALUES ('3-445-58668-5',6,7,'This book is a follow-up to Professor Hartpences book on how to carry 500 miles of fiber in your wallet.');
INSERT INTO bookreview VALUES ('4-344-22221-1',24,7,'A wonderful romance novel about Nelson, a successful editor for the New York Times, who resigns and retreats to a Dude Ranch in New Mexico to find himself.  Nelson meets Laura, a person with cowgirl charm in touch with her masculine self. Nelson falls in');
INSERT INTO bookreview VALUES ('4-668-39283-8',6,6,'A sometimes boring autobiography of a Texan transplanted to Rochester New York where he becomes a professor of Information Technology at RIT. Other than the chapters devoted to the Dallas Cowboys (chapters 1, 2, 3, 6, 10, 11, 12, 13), The Presidential ele');
INSERT INTO bookreview VALUES ('5-538-55746-1',15,9,'A wonderful romance novel about Laura, a successful editor for the New York Times, who resigns and retreats to a Dude Ranch in New Mexico to find herself.  Laura meets Nelson, a person with tough cowboy swagger in touch with his feminine self. Laura falls');
INSERT INTO bookreview VALUES ('6-449-68668-8',20,7,'The funny, often racy, autobiography of Daryl Johnson and his escapades to and from RIT. The book is largely about the excitement of commuting from the west side to RIT every day and how much fun it can be. A great book for those who live on the west side');
INSERT INTO bookreview VALUES ('6-449-68668-8',26,10,'An absolutely outstanding autobiography of a Saint John Fisher Graduate and how he has adapted to his 20 minute ride (mostly on 490) to RIT every morning and his 20 ride (mostly on 490) home from RIT every night. The author uses thirteen chapters to vibra');
INSERT INTO bookreview VALUES ('8-330-69586-6',5,10,'Geez .. no pages, no paragraphs, no words .. not even an illustration. But he did dedicate the book to Tony Clark. Says something for the fervor of this Detroit Tigers fan.');
INSERT INTO bookreview VALUES ('8-330-69586-6',15,10,'Very accurate analysis of a problem that is not NP complete.');
INSERT INTO bookreview VALUES ('9-333-44441-1',24,7,'Throwing the usual themes and twists of a romance novel to the winds, the author tells the store of Laura, a successful Dude Ranch owner in New Mexico, who resigns and moves to New York to seek her fortune as an editor for the New York Times. Laura meets');
INSERT INTO bookreview VALUES ('9-337-55467-3',25,9,'These authors know their way around a coding pad, let me tell you! In clear, bell-shaped tones, with succinct wording and pinpoint accuracy, they nail this subject matter to the wall. Do you like inheritance? How about information hiding? Maybe you like e');
INSERT INTO bookreview VALUES ('9-455-61223-3',13,9,'Professor Sonstein shows how oneness with the spiritual universe can be achieved by bringing the soul into rhytmn with the JAVA runtime machine. He shows that through various forms of self denial, such as writing code without pointers and heavy weight com');

我需要帮助显示评论者的名字,书的标题和评论者给出的评分。 但只有EmployedBy字段的审核人员才为空。

这应该是这样的:

 name  | title                                 | rating 
 Leone | How to Keep your Cellular Bill Down   |      7 
 Leone | How to Keep your Cable Bill Down      |      7 
 Leone | From Deep in the Heart of Texas to IT |      6 

我是否需要使用内部联接,联盟或仅使用多个SELECT?

1 个答案:

答案 0 :(得分:3)

使用旧版本(4.24)的MySql测试数据,这应该是您的查询

SELECT r.Name, b.Title, br.Rating
FROM Reviewer r JOIN BookReview br on br.ReviewerID = r.ReviewerID
     JOIN Book b ON b.ISBN = br.ISBN
WHERE r.EmployedBy IS NULL OR r.EmployedBy = ''

这是结果

'Leone','如何保持你的移动电话费用',​​7
'Leone','如何保持你的电缆收费',7
“Leone”,“从德克萨斯州的心脏深处到IT”,6