最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
sql IN 与not in的用法
时间:2022-11-14 23:37:44 编辑:袖梨 来源:一聚教程网
sql IN 与not in的用法
IN 操作符允许我们在 WHERE 子句中规定多个值。
SQL IN 语法
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)
实例
mysql教程> CREATE TABLE Topic(
-> TopicID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> Name VARCHAR(50) NOT NULL,
-> InStock SMALLINT UNSIGNED NOT NULL,
-> OnOrder SMALLINT UNSIGNED NOT NULL,
-> Reserved SMALLINT UNSIGNED NOT NULL,
-> Department ENUM('Classical', 'Popular') NOT NULL,
-> Category VARCHAR(20) NOT NULL,
-> RowUpdate TIMESTAMP NOT NULL
-> );
Query OK, 0 rows affected (0.03 sec)
mysql>
mysql>
mysql> INSERT INTO Topic (Name, InStock, OnOrder, Reserved, Department, Category) VALUES
-> ('Java', 10, 5, 3, 'Popular', 'Rock'),
-> ('JavaScript', 10, 5, 3, 'Classical', 'Opera'),
-> ('C Sharp', 17, 4, 1, 'Popular', 'Jazz'),
-> ('C', 9, 4, 2, 'Classical', 'Dance'),
-> ('C++', 24, 2, 5, 'Classical', 'General'),
-> ('Perl', 16, 6, 8, 'Classical', 'Vocal'),
-> ('Python', 2, 25, 6, 'Popular', 'Blues'),
-> ('Php', 32, 3, 10, 'Popular', 'Jazz'),
-> ('ASP.net', 12, 15, 13, 'Popular', 'Country'),
-> ('VB.net', 5, 20, 10, 'Popular', 'New Age'),
-> ('VC.net', 24, 11, 14, 'Popular', 'New Age'),
-> ('UML', 42, 17, 17, 'Classical', 'General'),
-> ('www.111com.net',25, 44, 28, 'Classical', 'Dance'),
-> ('Oracle', 32, 15, 12, 'Classical', 'General'),
-> ('Pl/SQL', 20, 10, 5, 'Classical', 'Opera'),
-> ('Sql Server', 23, 12, 8, 'Classical', 'General');
Query OK, 16 rows affected (0.00 sec)
Records: 16 Duplicates: 0 Warnings: 0
mysql>
mysql> select * from Topic;
+---------+----------------+---------+---------+----------+------------+----------+---------------------+
| TopicID | Name | InStock | OnOrder | Reserved | Department | Category | RowUpdate |
+---------+----------------+---------+---------+----------+------------+----------+---------------------+
| 1 | Java | 10 | 5 | 3 | Popular | Rock | 2007-07-23 19:09:47 |
| 2 | JavaScript | 10 | 5 | 3 | Classical | Opera | 2007-07-23 19:09:47 |
| 3 | C Sharp | 17 | 4 | 1 | Popular | Jazz | 2007-07-23 19:09:47 |
| 4 | C | 9 | 4 | 2 | Classical | Dance | 2007-07-23 19:09:47 |
| 5 | C++ | 24 | 2 | 5 | Classical | General | 2007-07-23 19:09:47 |
| 6 | Perl | 16 | 6 | 8 | Classical | Vocal | 2007-07-23 19:09:47 |
| 7 | Python | 2 | 25 | 6 | Popular | Blues | 2007-07-23 19:09:47 |
| 8 | Php | 32 | 3 | 10 | Popular | Jazz | 2007-07-23 19:09:47 |
| 9 | ASP.net | 12 | 15 | 13 | Popular | Country | 2007-07-23 19:09:47 |
| 10 | VB.net | 5 | 20 | 10 | Popular | New Age | 2007-07-23 19:09:47 |
| 11 | VC.net | 24 | 11 | 14 | Popular | New Age | 2007-07-23 19:09:47 |
| 12 | UML | 42 | 17 | 17 | Classical | General | 2007-07-23 19:09:47 |
| 13 | www.111com.net | 25 | 44 | 28 | Classical | Dance | 2007-07-23 19:09:47 |
| 14 | Oracle | 32 | 15 | 12 | Classical | General | 2007-07-23 19:09:47 |
| 15 | Pl/SQL | 20 | 10 | 5 | Classical | Opera | 2007-07-23 19:09:47 |
| 16 | Sql Server | 23 | 12 | 8 | Classical | General | 2007-07-23 19:09:47 |
+---------+----------------+---------+---------+----------+------------+----------+---------------------+
16 rows in set (0.00 sec)
mysql>
mysql> SELECT Name, Category, InStock
-> FROM Topic
-> WHERE Category IN ('Blues', 'Jazz')
-> ORDER BY Name;
+---------+----------+---------+
| Name | Category | InStock |
+---------+----------+---------+
| C Sharp | Jazz | 17 |
| Php | Jazz | 32 |
| Python | Blues | 2 |
+---------+----------+---------+
3 rows in set (0.00 sec)
利用in多表查询
mysql> CREATE TABLE Books(
-> BookID SMALLINT NOT NULL PRIMARY KEY,
-> BookTitle VARCHAR(60) NOT NULL,
-> Copyright YEAR NOT NULL
-> )
-> ENGINE=INNODB;
Query OK, 0 rows affected (0.05 sec)
mysql>
mysql>
mysql> INSERT INTO Books VALUES (12786, 'Java', 1934),
-> (13331, 'MySQL', 1919),
-> (14356, 'PHP', 1966),
-> (15729, 'PERL', 1932),
-> (16284, 'Oracle', 1996),
-> (17695, 'Pl/SQL', 1980),
-> (19264, 'JavaScript', 1992),
-> (19354, 'www.111com.net', 1993);
Query OK, 8 rows affected (0.03 sec)
Records: 8 Duplicates: 0 Warnings: 0
mysql>
mysql>
mysql> CREATE TABLE Authors(
-> AuthID SMALLINT NOT NULL PRIMARY KEY,
-> AuthFN VARCHAR(20),
-> AuthMN VARCHAR(20),
-> AuthLN VARCHAR(20)
-> )
-> ENGINE=INNODB;
Query OK, 0 rows affected (0.03 sec)
mysql>
mysql>
mysql> INSERT INTO Authors VALUES (1006, 'H', 'S.', 'T'),
-> (1007, 'J', 'C', 'O'),
-> (1008, 'B', NULL, 'E'),
-> (1009, 'R', 'M', 'R'),
-> (1010, 'J', 'K', 'T'),
-> (1011, 'J', 'G.', 'N'),
-> (1012, 'A', NULL, 'P'),
-> (1013, 'A', NULL, 'W'),
-> (1014, 'N', NULL, 'A');
Query OK, 9 rows affected (0.03 sec)
Records: 9 Duplicates: 0 Warnings: 0
mysql>
mysql>
mysql> CREATE TABLE AuthorBook(
-> AuthID SMALLINT NOT NULL,
-> BookID SMALLINT NOT NULL,
-> PRIMARY KEY (AuthID, BookID),
-> FOREIGN KEY (AuthID) REFERENCES Authors (AuthID),
-> FOREIGN KEY (BookID) REFERENCES Books (BookID)
-> )
-> ENGINE=INNODB;
Query OK, 0 rows affected (0.09 sec)
mysql>
mysql>
mysql> INSERT INTO AuthorBook VALUES (1006, 14356),
-> (1008, 15729),
-> (1009, 12786),
-> (1010, 17695),
-> (1011, 15729),
-> (1012, 19264),
-> (1012, 19354),
-> (1014, 16284);
Query OK, 8 rows affected (0.03 sec)
Records: 8 Duplicates: 0 Warnings: 0
mysql>
mysql>
mysql> select * from Authors;
+--------+--------+--------+--------+
| AuthID | AuthFN | AuthMN | AuthLN |
+--------+--------+--------+--------+
| 1006 | H | S. | T |
| 1007 | J | C | O |
| 1008 | B | NULL | E |
| 1009 | R | M | R |
| 1010 | J | K | T |
| 1011 | J | G. | N |
| 1012 | A | NULL | P |
| 1013 | A | NULL | W |
| 1014 | N | NULL | A |
+--------+--------+--------+--------+
9 rows in set (0.00 sec)
mysql> select * from Books;
+--------+----------------+-----------+
| BookID | BookTitle | Copyright |
+--------+----------------+-----------+
| 12786 | Java | 1934 |
| 13331 | MySQL | 1919 |
| 14356 | PHP | 1966 |
| 15729 | PERL | 1932 |
| 16284 | Oracle | 1996 |
| 17695 | Pl/SQL | 1980 |
| 19264 | JavaScript | 1992 |
| 19354 | www.111com.net | 1993 |
+--------+----------------+-----------+
8 rows in set (0.00 sec)
mysql> select * from AuthorBook;
+--------+--------+
| AuthID | BookID |
+--------+--------+
| 1009 | 12786 |
| 1006 | 14356 |
| 1008 | 15729 |
| 1011 | 15729 |
| 1014 | 16284 |
| 1010 | 17695 |
| 1012 | 19264 |
| 1012 | 19354 |
+--------+--------+
8 rows in set (0.01 sec)
mysql>
mysql> SELECT BookTitle, Copyright
-> FROM Books
-> WHERE Copyright NOT IN
-> (
-> SELECT b.copyright
-> FROM Books AS b JOIN AuthorBook AS ab USING (BookID)
-> JOIN Authors AS a USING (AuthID)
-> WHERE AuthLN='MySQL'
-> )
-> ORDER BY BookTitle;
+----------------+-----------+
| BookTitle | Copyright |
+----------------+-----------+
| Java | 1934 |
| JavaScript | 1992 |
| MySQL | 1919 |
| Oracle | 1996 |
| PERL | 1932 |
| PHP | 1966 |
| Pl/SQL | 1980 |
| www.111com.net | 1993 |
+----------------+-----------+
相关文章
- 人们熟悉的寄居蟹属于以下哪种分类 神奇海洋11月21日答案 11-21
- 第五人格11.22共研服有什么更新 11月22日共研服更新内容介绍 11-21
- 原神恰斯卡怎么培养 11-21
- 无期迷途四星装束是谁 11-21
- 王者荣耀帝丹高中校服怎么获得 11-21
- 光遇姆明季后续版本怎么玩 11-21