Join three tables : Simple JOIN « Join « SQL / MySQL






Join three tables

  
/*
mysql> SELECT ArticleTitle, Copyright, CONCAT_WS(' ', AuthorFirstName, AuthorMid
dleName, AuthorLastName) AS Author
    -> FROM Articles AS b, AuthorArticle AS ab, Authors AS a
    -> WHERE b.ArticleID=ab.ArticleID AND ab.AuthID=a.AuthID AND Copyright<1980
    -> ORDER BY ArticleTitle;
+-------------------+-----------+-------------------+
| ArticleTitle      | Copyright | Author            |
+-------------------+-----------+-------------------+
| Buy a paper       |      1932 | James Elk         |
| Buy a paper       |      1932 | Mary G. Lee       |
| How write a paper |      1934 | Tom M Ride        |
| Sell a paper      |      1966 | Henry S. Thompson |
+-------------------+-----------+-------------------+
4 rows in set (0.00 sec)

mysql>
*/

Drop table Articles;
Drop table Authors;
Drop table AuthorArticle;



CREATE TABLE Articles (
   ArticleID SMALLINT NOT NULL PRIMARY KEY,
   ArticleTitle VARCHAR(60) NOT NULL,
   Copyright YEAR NOT NULL
)
ENGINE=INNODB;


INSERT INTO Articles VALUES (12786, 'How write a paper', 1934),
                            (13331, 'Publish a paper', 1919),
                            (14356, 'Sell a paper', 1966),
                            (15729, 'Buy a paper', 1932),
                            (16284, 'Conferences', 1996),
                            (17695, 'Journal', 1980),
                            (19264, 'Information', 1992),
                            (19354, 'AI', 1993);


CREATE TABLE Authors (
   AuthID SMALLINT NOT NULL PRIMARY KEY,
   AuthorFirstName VARCHAR(20),
   AuthorMiddleName VARCHAR(20),
   AuthorLastName VARCHAR(20)
)
ENGINE=INNODB;


INSERT INTO Authors VALUES (1006, 'Henry', 'S.', 'Thompson'),
                           (1007, 'Jason', 'Carol', 'Oak'),
                           (1008, 'James', NULL, 'Elk'),
                           (1009, 'Tom', 'M', 'Ride'),
                           (1010, 'Jack', 'K', 'Ken'),
                           (1011, 'Mary', 'G.', 'Lee'),
                           (1012, 'Annie', NULL, 'Peng'),
                           (1013, 'Alan', NULL, 'Wang'),
                           (1014, 'Nelson', NULL, 'Yin');


CREATE TABLE AuthorArticle (
   AuthID SMALLINT NOT NULL,
   ArticleID SMALLINT NOT NULL,
   PRIMARY KEY (AuthID, ArticleID),
   FOREIGN KEY (AuthID) REFERENCES Authors (AuthID),
   FOREIGN KEY (ArticleID) REFERENCES Articles (ArticleID)
)
ENGINE=INNODB;


INSERT INTO AuthorArticle VALUES (1006, 14356), 
                              (1008, 15729), 
                              (1009, 12786), 
                              (1010, 17695),
                              (1011, 15729), 
                              (1012, 19264), 
                              (1012, 19354), 
                              (1014, 16284);
  
SELECT ArticleTitle, Copyright, CONCAT_WS(' ', AuthorFirstName, AuthorMiddleName, AuthorLastName) AS Author
FROM Articles AS b, AuthorArticle AS ab, Authors AS a
WHERE b.ArticleID=ab.ArticleID AND ab.AuthID=a.AuthID AND Copyright<1980
ORDER BY ArticleTitle;

           
         
    
  








Related examples in the same category

1.Using More Than one Table
2.Self join
3.Simple table join
4.Query data from two tables
5.JOIN two tables with alias name
6.Using a Join to Control Query Output Order
7.Using a Join to Create a Lookup Table from Descriptive Labels
8.Return the first names and surnames of both the sales rep and the customer, as well as the value of the sale
9.Query data from two tables 2
10.Finding Rows in One Table That Match Rows in Another
11.Identify records from author table that corresponds to the author name, use its a_id value to find matching re
12.Using information in the book table to find information in the author table
13.Finding Rows with No Match in Another Table
14.Shorten the output column list to include only columns from the author table
15.List each author from the author table, and whether or not you have any books by the author
16.Using table alias to qualify column name when column names exist
17.Using table alias to qualify column name
18.PSEUDONYMS FOR TABLE NAMES
19.Qualify the column name with table name