Select distinct records using JOIN : Distinct « Select Clause « SQL / MySQL






Select distinct records using JOIN

   
/*
mysql> SELECT CONCAT_WS(' ', AuthorFirstName, AuthorMiddleName, AuthorLastName)
AS Author
    -> FROM Authors
    -> WHERE AuthID=
    ->    (
    ->       SELECT ab.AuthID
    ->       FROM AuthorArticle AS ab, Articles AS b
    ->       WHERE ab.ArticleID=b.ArticleID AND ArticleTitle='AI'
    ->    );
+-------------+
| Author      |
+-------------+
| Annie Watts |
+-------------+
1 row in set (0.02 sec)

*/       
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 DISTINCT CONCAT_WS(' ', AuthorFirstName, AuthorMiddleName, AuthorLastName) AS Author
FROM Authors AS a JOIN AuthorArticle AS ab ON a.AuthID=ab.AuthID
   JOIN Articles AS b ON ab.ArticleID=b.ArticleID
WHERE ArticleTitle='AI';



           
         
    
    
  








Related examples in the same category

1.Use DISTINCT to get non-dupliate records
2.Use DISTICNT to get unique value
3.Eliminating Duplicate Data Using DISTINCT 1
4.Eliminating Duplicate Data Using DISTINCT 2
5.The SQL key word DISTINCT has the effect that equivalent data records are output only once.
6.DISTINCT works with multiple-column output too.
7.how many different drivers there are, use COUNT(DISTINCT)
8.DISTINCT with two columns
9.DISTINCT works with expressions, not just column values.
10.Inserting name values into the multisequence table generates separate sequences for each distinct name:
11.Count distinct
12.Distinct sub string
13.Average distinct value
14.Return a list of surnames, with each surname appearing only once?
15.The fraction of the records that contain unique or non-unique names: