SQL Server/T-SQL Tutorial/Query/In

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

An IN operator is equivalent to a series of conditions, connected with one or more OR operators.

4> CREATE TABLE employee(
5>    id          INTEGER NOT NULL PRIMARY KEY,
6>    first_name  VARCHAR(10),
7>    last_name   VARCHAR(10),
8>    salary      DECIMAL(10,2),
9>    start_Date  DATETIME,
10>    region      VARCHAR(10),
11>    city        VARCHAR(20),
12>    managerid   INTEGER
13> );
14> GO
1> INSERT INTO employee VALUES (1, "Jason" ,  "Martin", 5890,"2005-03-22","North","Vancouver",3);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (2, "Alison",  "Mathews",4789,"2003-07-21","South","Utown",4);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (3, "James" ,  "Smith",  6678,"2001-12-01","North","Paris",5);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (4, "Celia" ,  "Rice",   5567,"2006-03-03","South","London",6);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (5, "Robert",  "Black",  4467,"2004-07-02","East","Newton",7);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (6, "Linda" ,  "Green" , 6456,"2002-05-19","East","Calgary",8);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (7, "David" ,  "Larry",  5345,"2008-03-18","West","New York",9);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (8, "James" ,  "Cat",    4234,"2007-07-17","West","Regina",9);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (9, "Joan"  ,  "Act",    6123,"2001-04-16","North","Toronto",10);
2> GO
(1 rows affected)
1>
2> select * from employee;
3> GO
id          first_name last_name  salary       start_Date              region     city                 managerid
----------- ---------- ---------- ------------ ----------------------- ---------- -------------------- -----------
          1 Jason      Martin          5890.00 2005-03-22 00:00:00.000 North      Vancouver                      3
          2 Alison     Mathews         4789.00 2003-07-21 00:00:00.000 South      Utown                          4
          3 James      Smith           6678.00 2001-12-01 00:00:00.000 North      Paris                          5
          4 Celia      Rice            5567.00 2006-03-03 00:00:00.000 South      London                         6
          5 Robert     Black           4467.00 2004-07-02 00:00:00.000 East       Newton                         7
          6 Linda      Green           6456.00 2002-05-19 00:00:00.000 East       Calgary                        8
          7 David      Larry           5345.00 2008-03-18 00:00:00.000 West       New York                       9
          8 James      Cat             4234.00 2007-07-17 00:00:00.000 West       Regina                         9
          9 Joan       Act             6123.00 2001-04-16 00:00:00.000 North      Toronto                       10
(9 rows affected)
1>
2> SELECT * FROM employee
3>        WHERE ID = 2
4>        OR ID = 8
5>        OR ID = 5
6>
7>
8> drop table employee;
9> GO
id          first_name last_name  salary       start_Date              region     city                 managerid
----------- ---------- ---------- ------------ ----------------------- ---------- -------------------- -----------
          2 Alison     Mathews         4789.00 2003-07-21 00:00:00.000 South      Utown                          4
          5 Robert     Black           4467.00 2004-07-02 00:00:00.000 East       Newton                         7
          8 James      Cat             4234.00 2007-07-17 00:00:00.000 West       Regina                         9
(3 rows affected)
1>
2>


An IN phrase preceded by NOT

use the NOT operator to test for an expression that"s not in the list of expressions.
6>
7>
8> create table Bankers(
9>    BankerID             Integer,
10>    BankerName           VARCHAR(20),
11>    BankerContactLName   VARCHAR(20),
12>    BankerContactFName   VARCHAR(20),
13>    BankerCity           VARCHAR(20),
14>    BankerState          VARCHAR(20),
15>    BankerZipCode        VARCHAR(20),
16>    BankerPhone          VARCHAR(20)
17> )
18> GO
1>
2> insert into Bankers values (1, "ABC Inc.","Joe","Smith","Vancouver","BC","11111","111-111-1111");
3> GO
(1 rows affected)
1> insert into Bankers values (2, "DEF Inc.","Red","Rice", "New York", "DE","22222","222-222-2222");
2> GO
(1 rows affected)
1> insert into Bankers values (3, "HJI Inc.","Kit","Cat",  "Paris",    "CA","33333","333-333-3333");
2> GO
(1 rows affected)
1> insert into Bankers values (4, "QWE Inc.","Git","Black","Regina",   "ER","44444","444-444-4444");
2> GO
(1 rows affected)
1> insert into Bankers values (5, "RTY Inc.","Wil","Lee",  "Toronto",  "YU","55555","555-555-5555");
2> GO
(1 rows affected)
1> insert into Bankers values (6, "YUI Inc.","Ted","Larry","Calgary",  "TY","66666","666-666-6666");
2> GO
(1 rows affected)
1> insert into Bankers values (7, "OIP Inc.","Yam","Act",  "San Franc","FG","77777","777-777-7777");
2> GO
(1 rows affected)
1> insert into Bankers values (8, "SAD Inc.","Hit","Eat",  "Orland",   "PO","88888","888-888-8888");
2> GO
(1 rows affected)
1> insert into Bankers values (9, "DFG Inc.","Sad","Lee",  "Wisler",   "PL","99999","999-999-9999");
2> GO
(1 rows affected)
1> insert into Bankers values (0, "GHJ Inc.","Bit","Lee",  "Ticker",   "MN","00000","000-000-0000");
2> GO
(1 rows affected)
1>
2> select * from Bankers WHERE BankerState NOT IN ("CA", "NV", "OR")
3> GO
BankerID    BankerName           BankerContactLName   BankerContactFName   BankerCity           BankerState          BankerZipCode        BankerPhone
----------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- --------------------
          1 ABC Inc.             Joe                  Smith                Vancouver            BC                   11111                111-111-1111
          2 DEF Inc.             Red                  Rice                 New York             DE                   22222                222-222-2222
          4 QWE Inc.             Git                  Black                Regina               ER                   44444                444-444-4444
          5 RTY Inc.             Wil                  Lee                  Toronto              YU                   55555                555-555-5555
          6 YUI Inc.             Ted                  Larry                Calgary              TY                   66666                666-666-6666
          7 OIP Inc.             Yam                  Act                  San Franc            FG                   77777                777-777-7777
          8 SAD Inc.             Hit                  Eat                  Orland               PO                   88888                888-888-8888
          9 DFG Inc.             Sad                  Lee                  Wisler               PL                   99999                999-999-9999
          0 GHJ Inc.             Bit                  Lee                  Ticker               MN                   00000                000-000-0000
(9 rows affected)
1>
2> drop table Bankers;
3> GO
1>
2>
3>
4>


An IN phrase with a list of numeric literals

use the IN phrase to test whether an expression is equal to a value in a list of expressions.
16> create table Billings (
17>     BankerID           INTEGER,
18>     BillingNumber      INTEGER,
19>     BillingDate        datetime,
20>     BillingTotal       INTEGER,
21>     TermsID            INTEGER,
22>     BillingDueDate     datetime ,
23>     PaymentTotal       INTEGER,
24>     CreditTotal        INTEGER
25>
26> );
27> GO
1>
2> INSERT INTO Billings VALUES (1, 1, "2005-01-22", 165, 1,"2005-04-22",123,321);
3> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (2, 2, "2001-02-21", 165, 1,"2002-02-22",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (3, 3, "2003-05-02", 165, 1,"2005-04-12",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (4, 4, "1999-03-12", 165, 1,"2005-04-18",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (5, 5, "2000-04-23", 165, 1,"2005-04-17",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (6, 6, "2001-06-14", 165, 1,"2005-04-18",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (7, 7, "2002-07-15", 165, 1,"2005-04-19",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (8, 8, "2003-08-16", 165, 1,"2005-04-20",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (9, 9, "2004-09-17", 165, 1,"2005-04-21",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (0, 0, "2005-10-18", 165, 1,"2005-04-22",123,321);
2> GO
(1 rows affected)
1>
2> select * from Billings WHERE TermsID IN (1, 3, 4)
3> GO
BankerID    BillingNumber BillingDate             BillingTotal TermsID     BillingDueDate          PaymentTotal CreditTotal
----------- ------------- ----------------------- ------------ ----------- ----------------------- ------------ -----------
          1             1 2005-01-22 00:00:00.000          165           1 2005-04-22 00:00:00.000          123         321
          2             2 2001-02-21 00:00:00.000          165           1 2002-02-22 00:00:00.000          123         321
          3             3 2003-05-02 00:00:00.000          165           1 2005-04-12 00:00:00.000          123         321
          4             4 1999-03-12 00:00:00.000          165           1 2005-04-18 00:00:00.000          123         321
          5             5 2000-04-23 00:00:00.000          165           1 2005-04-17 00:00:00.000          123         321
          6             6 2001-06-14 00:00:00.000          165           1 2005-04-18 00:00:00.000          123         321
          7             7 2002-07-15 00:00:00.000          165           1 2005-04-19 00:00:00.000          123         321
          8             8 2003-08-16 00:00:00.000          165           1 2005-04-20 00:00:00.000          123         321
          9             9 2004-09-17 00:00:00.000          165           1 2005-04-21 00:00:00.000          123         321
          0             0 2005-10-18 00:00:00.000          165           1 2005-04-22 00:00:00.000          123         321
(10 rows affected)
1>
2> drop table Billings;
3> GO


An IN phrase with a subquery

9>
10> create table Billings (
11>     BankerID           INTEGER,
12>     BillingNumber      INTEGER,
13>     BillingDate        datetime,
14>     BillingTotal       INTEGER,
15>     TermsID            INTEGER,
16>     BillingDueDate     datetime ,
17>     PaymentTotal       INTEGER,
18>     CreditTotal        INTEGER
19>
20> );
21> GO
1>
2> INSERT INTO Billings VALUES (1, 1, "2005-01-22", 165, 1,"2005-04-22",123,321);
3> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (2, 2, "2001-02-21", 165, 1,"2002-02-22",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (3, 3, "2003-05-02", 165, 1,"2005-04-12",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (4, 4, "1999-03-12", 165, 1,"2005-04-18",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (5, 5, "2000-04-23", 165, 1,"2005-04-17",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (6, 6, "2001-06-14", 165, 1,"2005-04-18",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (7, 7, "2002-07-15", 165, 1,"2005-04-19",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (8, 8, "2003-08-16", 165, 1,"2005-04-20",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (9, 9, "2004-09-17", 165, 1,"2005-04-21",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (0, 0, "2005-10-18", 165, 1,"2005-04-22",123,321);
2> GO
(1 rows affected)
1>
2>
3> create table Bankers(
4>    BankerID             Integer,
5>    BankerName           VARCHAR(20),
6>    BankerContactLName   VARCHAR(20),
7>    BankerContactFName   VARCHAR(20),
8>    BankerCity           VARCHAR(20),
9>    BankerState          VARCHAR(20),
10>    BankerZipCode        VARCHAR(20),
11>    BankerPhone          VARCHAR(20)
12> )
13> GO
1>
2> insert into Bankers values (1, "ABC Inc.","Joe","Smith","Vancouver","BC","11111","111-111-1111");
3> GO
(1 rows affected)
1> insert into Bankers values (2, "DEF Inc.","Red","Rice", "New York", "DE","22222","222-222-2222");
2> GO
(1 rows affected)
1> insert into Bankers values (3, "HJI Inc.","Kit","Cat",  "Paris",    "CA","33333","333-333-3333");
2> GO
(1 rows affected)
1> insert into Bankers values (4, "QWE Inc.","Git","Black","Regina",   "ER","44444","444-444-4444");
2> GO
(1 rows affected)
1> insert into Bankers values (5, "RTY Inc.","Wil","Lee",  "Toronto",  "YU","55555","555-555-5555");
2> GO
(1 rows affected)
1> insert into Bankers values (6, "YUI Inc.","Ted","Larry","Calgary",  "TY","66666","666-666-6666");
2> GO
(1 rows affected)
1> insert into Bankers values (7, "OIP Inc.","Yam","Act",  "San Franc","FG","77777","777-777-7777");
2> GO
(1 rows affected)
1> insert into Bankers values (8, "SAD Inc.","Hit","Eat",  "Orland",   "PO","88888","888-888-8888");
2> GO
(1 rows affected)
1> insert into Bankers values (9, "DFG Inc.","Sad","Lee",  "Wisler",   "PL","99999","999-999-9999");
2> GO
(1 rows affected)
1> insert into Bankers values (0, "GHJ Inc.","Bit","Lee",  "Ticker",   "MN","00000","000-000-0000");
2> GO
(1 rows affected)
1>
2>
3>
4>
5>
6> select * from Bankers WHERE BankerID IN
7>     (SELECT BankerID
8>      FROM Billings
9>      WHERE BillingDate = "2002-05-01")
10> GO
BankerID    BankerName           BankerContactLName   BankerContactFName   BankerCity           BankerState          BankerZipCode        BankerPhone
----------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- --------------------
(0 rows affected)
1>
2>
3> drop table Bankers;
4> GO
1>
2> drop table Billings;
3> GO


In and subquery

6>
7> create table Billings (
8>     BankerID           INTEGER,
9>     BillingNumber      INTEGER,
10>     BillingDate        datetime,
11>     BillingTotal       INTEGER,
12>     TermsID            INTEGER,
13>     BillingDueDate     datetime ,
14>     PaymentTotal       INTEGER,
15>     CreditTotal        INTEGER
16>
17> );
18> GO
1>
2> INSERT INTO Billings VALUES (1, 1, "2005-01-22", 165, 1,"2005-04-22",123,321);
3> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (2, 2, "2001-02-21", 165, 1,"2002-02-22",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (3, 3, "2003-05-02", 165, 1,"2005-04-12",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (4, 4, "1999-03-12", 165, 1,"2005-04-18",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (5, 5, "2000-04-23", 165, 1,"2005-04-17",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (6, 6, "2001-06-14", 165, 1,"2005-04-18",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (7, 7, "2002-07-15", 165, 1,"2005-04-19",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (8, 8, "2003-08-16", 165, 1,"2005-04-20",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (9, 9, "2004-09-17", 165, 1,"2005-04-21",123,321);
2> GO
(1 rows affected)
1> INSERT INTO Billings VALUES (0, 0, "2005-10-18", 165, 1,"2005-04-22",123,321);
2> GO
(1 rows affected)
1>
2>
3>
4> create table Bankers(
5>    BankerID             Integer,
6>    BankerName           VARCHAR(20),
7>    BankerContactLName   VARCHAR(20),
8>    BankerContactFName   VARCHAR(20),
9>    BankerCity           VARCHAR(20),
10>    BankerState          VARCHAR(20),
11>    BankerZipCode        VARCHAR(20),
12>    BankerPhone          VARCHAR(20)
13> )
14> GO
1>
2> insert into Bankers values (1, "ABC Inc.","Joe","Smith","Vancouver","BC","11111","111-111-1111");
3> GO
(1 rows affected)
1> insert into Bankers values (2, "DEF Inc.","Red","Rice", "New York", "DE","22222","222-222-2222");
2> GO
(1 rows affected)
1> insert into Bankers values (3, "HJI Inc.","Kit","Cat",  "Paris",    "CA","33333","333-333-3333");
2> GO
(1 rows affected)
1> insert into Bankers values (4, "QWE Inc.","Git","Black","Regina",   "ER","44444","444-444-4444");
2> GO
(1 rows affected)
1> insert into Bankers values (5, "RTY Inc.","Wil","Lee",  "Toronto",  "YU","55555","555-555-5555");
2> GO
(1 rows affected)
1> insert into Bankers values (6, "YUI Inc.","Ted","Larry","Calgary",  "TY","66666","666-666-6666");
2> GO
(1 rows affected)
1> insert into Bankers values (7, "OIP Inc.","Yam","Act",  "San Franc","FG","77777","777-777-7777");
2> GO
(1 rows affected)
1> insert into Bankers values (8, "SAD Inc.","Hit","Eat",  "Orland",   "PO","88888","888-888-8888");
2> GO
(1 rows affected)
1> insert into Bankers values (9, "DFG Inc.","Sad","Lee",  "Wisler",   "PL","99999","999-999-9999");
2> GO
(1 rows affected)
1> insert into Bankers values (0, "GHJ Inc.","Bit","Lee",  "Ticker",   "MN","00000","000-000-0000");
2> GO
(1 rows affected)
1>
2>
3>
4> UPDATE Billings
5> SET TermsID = 1
6> WHERE BankerID IN
7>    (SELECT BankerID
8>     FROM Bankers
9>     WHERE BankerState IN ("CA", "AZ", "NV"))
10>
11> GO
(1 rows affected)
1>
2> drop table Bankers;
3> drop table Billings;
4> GO


The syntax of the WHERE clause with an IN phrase

WHERE test_expression [NOT] IN ({subquery|expression_1 [, expression_2]...})


use subqueries to restrict what rows in a table will be deleted

3>
4> CREATE TABLE titles(
5>    title_id       varchar(20),
6>    title          varchar(80)       NOT NULL,
7>    type           char(12)          NOT NULL,
8>    pub_id         char(4)               NULL,
9>    price          money                 NULL,
10>    advance        money                 NULL,
11>    royalty        int                   NULL,
12>    ytd_sales      int                   NULL,
13>    notes          varchar(200)          NULL,
14>    pubdate        datetime          NOT NULL
15> )
16> GO
1>
2> CREATE TABLE sales(
3>    stor_id        char(4)           NOT NULL,
4>    ord_num        varchar(20)       NOT NULL,
5>    ord_date       datetime          NOT NULL,
6>    qty            smallint          NOT NULL,
7>    payterms       varchar(12)       NOT NULL,
8>    title_id       varchar(80)
9> )
10> GO
1> insert sales values("1", "QA7442.3", "09/13/94", 75, "ON Billing","1")
2> insert sales values("2", "D4482",    "09/14/94", 10, "Net 60",    "1")
3> insert sales values("3", "N914008",  "09/14/94", 20, "Net 30",    "2")
4> insert sales values("4", "N914014",  "09/14/94", 25, "Net 30",    "3")
5> insert sales values("5", "423LL922", "09/14/94", 15, "ON Billing","3")
6> insert sales values("6", "423LL930", "09/14/94", 10, "ON Billing","2")
7> GO
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
(1 rows affected)
1>
2>      DELETE    sales
3>      WHERE     title_id IN
4>                (SELECT    title_id from titles
5>                WHERE      pub_id = "1389")
6>
7>
8> GO
(0 rows affected)
1>
2> drop table sales;
3>
4> drop table titles;
5> GO