SQL/MySQL/Select Clause/Using — различия между версиями

Материал из SQL эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 10:16, 26 мая 2010

USING command in JOIN

/*
mysql> SELECT ArticleTitle, Copyright, ab.AuthID
    -> FROM Articles JOIN AuthorArticle AS ab
    ->    USING (ArticleID)
    -> ORDER BY ArticleTitle;
+-------------------+-----------+--------+
| ArticleTitle      | Copyright | AuthID |
+-------------------+-----------+--------+
| AI                |      1993 |   1012 |
| Buy a paper       |      1932 |   1008 |
| Buy a paper       |      1932 |   1011 |
| Conferences       |      1996 |   1014 |
| How write a paper |      1934 |   1009 |
| Information       |      1992 |   1012 |
| Journal           |      1980 |   1010 |
| Sell a paper      |      1966 |   1006 |
+-------------------+-----------+--------+
8 rows in set (0.01 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 ArticleTitle, Copyright, ab.AuthID
FROM Articles JOIN AuthorArticle AS ab
   USING (ArticleID)
ORDER BY ArticleTitle;



Using IN command in sub query

/*
mysql> SELECT ArticleID FROM AuthorArticle WHERE AuthID IN
    ->    (
    ->       SELECT AuthID FROM AuthorArticle
    ->       GROUP BY AuthID
    ->       HAVING COUNT(*)>1
    ->    );
+-----------+
| ArticleID |
+-----------+
|     19264 |
|     19354 |
+-----------+
2 rows in set (0.04 sec)

*/
Drop table Articles;
Drop table Authors;
Drop table AuthorArticle;
Drop table ArticleOrders;

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);
CREATE TABLE ArticleOrders
(
   OrderID SMALLINT NOT NULL,
   ArticleID SMALLINT NOT NULL,
   Quantity SMALLINT NOT NULL,
   PRIMARY KEY (OrderID, ArticleID),
   FOREIGN KEY (ArticleID) REFERENCES Articles (ArticleID)
)
ENGINE=INNODB;

INSERT INTO ArticleOrders VALUES (101, 13331, 1), 
                                 (101, 12786, 1), 
                                 (101, 16284, 2), 
                                 (102, 19354, 1),
                                 (102, 15729, 3), 
                                 (103, 12786, 2), 
                                 (103, 19264, 1), 
                                 (103, 13331, 1),
                                 (103, 14356, 2), 
                                 (104, 19354, 1), 
                                 (105, 15729, 1), 
                                 (105, 14356, 2),
                                 (106, 16284, 2), 
                                 (106, 13331, 1), 
                                 (107, 12786, 3), 
                                 (108, 19354, 1),
                                 (108, 16284, 4), 
                                 (109, 15729, 1), 
                                 (110, 13331, 2), 
                                 (110, 12786, 2),
                                 (110, 14356, 2), 
                                 (111, 14356, 2);
    
SELECT ArticleID FROM AuthorArticle WHERE AuthID IN
   (
      SELECT AuthID FROM AuthorArticle
      GROUP BY AuthID
      HAVING COUNT(*)>1
   );