V tem članku SQL se boste naučili uporabljati ključno besedo IN v poizvedbah baze podatkov SQL.
Kaj je ključna beseda IN v SQL?
The IN je logični operator v jeziku strukturiranih poizvedb, ki omogoča uporabnikom baze podatkov, da definirajo več kot eno vrednost v klavzuli WHERE.
Klavzula WHERE z operatorjem IN prikazuje tiste zapise v rezultatu, ki se ujemajo z danim nizom vrednosti. Podpoizvedbo lahko podamo tudi v oklepaju operatorja IN.
Operator IN lahko uporabimo s poizvedbami INSERT, SELECT, UPDATE in DELETE v bazi podatkov SQL.
Operator IN v SQL nadomešča postopek več pogojev ALI v poizvedbah.
Sintaksa operatorja IN:
SELECT Column_Name_1, Column_Name_2, Column_Name_3, ......, Column_Name_N FROM Table_Name WHERE Column_Name IN (Value_1, Value_2, Value_3, ......., Value_N);
Če želite uporabiti operator IN v stavkih SQL, morate slediti spodnjim korakom v enakem zaporedju:
- Ustvari bazo podatkov v SQL.
- Ustvarite novo tabelo SQL.
- Podatke vnesite v tabelo.
- Oglejte si vstavljene podatke.
- Za prikaz podatkov tabele uporabite operator SQL IN.
Zdaj bomo na kratko razložili vsak korak enega za drugim z najboljšim primerom SQL:
1. korak: ustvarite preprosto novo bazo podatkov
Prvi korak je ustvariti novo bazo podatkov v jeziku strukturiranih poizvedb.
Naslednji stavek CREATE ustvari novo Strojna_višja šola Baza podatkov v strežniku SQL:
CREATE Database Mechanical_College;
2. korak: ustvarite novo tabelo
Zdaj uporabite naslednjo sintakso SQL, ki pomaga pri ustvarjanju nove tabele v bazi podatkov:
CREATE TABLE table_name ( 1st_Column data type (character_size of 1st Column), 2nd_Column data type (character_size of the 2nd column ), 3rd_Column data type (character_size of the 3rd column), ...
Nth_Column data type (character_size of the Nth column) );
Naslednji stavek CREATE ustvari Fakulteta_Info tabela v Strojna_višja šola Baza podatkov:
CREATE TABLE Faculty_Info ( Faculty_ID INT NOT NULL PRIMARY KEY, Faculty_First_Name VARCHAR (100), Faculty_Last_Name VARCHAR (100), Faculty_Dept_Id INT NOT NULL, Faculty_Joining_DateDATE, Faculty_City Varchar (80), Faculty_Salary INT );
3. korak: Vstavite podatke v tabelo
Naslednje poizvedbe INSERT vstavijo zapise fakultet v tabelo Faculty_Info:
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_NameFaculty_Dept_Id, Faculty_Joining_Date, Faculty_City, Faculty_Salary) VALUES (1001, Arush, Sharma, 4001, 2020-01-02, Delhi, 20000); INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_NameFaculty_Dept_Id, Faculty_Joining_Date, Faculty_City, Faculty_Salary) VALUES (1002, Bulbul, Roy, 4002, 2019-12-31, Delhi, 38000 ); INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_NameFaculty_Dept_Id, Faculty_Joining_Date, Faculty_City, Faculty_Salary) VALUES (1004, Saurabh, Sharma, 4001, 2020-10-10, Mumbai, 45000); INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_NameFaculty_Dept_Id, Faculty_Joining_Date, Faculty_City, Faculty_Salary) VALUES (1005, Shivani, Singhania, 4001, 2019-07-15, Kolkata, 42000); INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_NameFaculty_Dept_Id, Faculty_Joining_Date, Faculty_City, Faculty_Salary) VALUES (1006, Avinash, Sharma, 4002, 2019-11-11, Delhi, 28000); INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_NameFaculty_Dept_Id, Faculty_Joining_Date, Faculty_City, Faculty_Salary)VALUES (1007, Shyam, Besas, 4003, 2021-06-21, Lucknow, 35000);
4. korak: Oglejte si vstavljene podatke
Naslednji stavek SELECT prikaže podatke tabele Faculty_Info:
SELECT * FROM Faculty_Info;
Faculty_Id | Ime_fakultete | Fakulteta_priimek | Faculty_Dept_Id | Datum_vstopa na fakulteto | Fakulteta_Mesto | Fakulteta_Plača |
---|---|---|---|---|---|---|
1001 | medved | Sharma | 4001 | 2020-01-02 | Delhi | 20000 |
1002 | Bulbul | Roy | 4002 | 2019-12-31 | Delhi | 38000 |
1004 | Saurabh | Roy | 4001 | 2020-10-10 | Mumbaj | 45000 |
1005 | Shivani | Singhania | 4001 | 2019-07-15 | Kolkata | 42000 |
1006 | Avinaš | Sharma | 4002 | 2019-11-11 | Delhi | 28000 |
1007 | Shyam | Ti poljubi | 4003 | 2021-06-21 | Lucknow | 35000 |
5. korak: Uporabite operater IN za ogled podatkov tabele Faculty_Info na različne načine
Naslednja poizvedba uporablja številske vrednosti z operatorjem IN:
primerjaj z nizom
SELECT Faculty_Id, Faculty_First_Name, Faculty_Dept_Id, Faculty_Joining_Date, Faculty_Salary FROM Faculty_Info WHERE Faculty_Salary IN ( 38000, 42000, 45000, 35000);
Ta poizvedba prikazuje samo zapis tistih fakultet, katerih plača je podana v operatorju IN klavzule WHERE.
Izhod:
Faculty_Id | Ime_fakultete | Faculty_Dept_Id | Datum_vstopa na fakulteto | Fakulteta_Plača |
---|---|---|---|---|
1002 | Bulbul | 4002 | 2019-12-31 | 38000 |
1004 | Saurabh | 4001 | 2020-10-10 | 45000 |
1005 | Shivani | 4001 | 2019-07-15 | 42000 |
1007 | Shyam | 4003 | 2021-06-21 | 35000 |
Naslednja poizvedba uporablja besedilne ali znakovne vrednosti z logičnim operatorjem IN:
SELECT Faculty_Id, Faculty_First_Name, Faculty_Joining_Date, Faculty_City FROM Faculty_Info WHERE Faculty_City IN ( Mumbai, Kolkata, Lucknow);
Ta poizvedba prikazuje samo zapis tistih fakultet, katerih mesto je vključeno v oklepaj operatorja IN v stavku WHERE.
Izhod:
Faculty_Id | Ime_fakultete | Datum_vstopa na fakulteto | Fakulteta_Mesto |
---|---|---|---|
1004 | Saurabh | 2020-10-10 | Mumbaj |
1005 | Shivani | 2019-07-15 | Kolkata |
1007 | Shyam | 2021-06-21 | Lucknow |
Naslednja poizvedba uporablja format DATE z logičnim operatorjem IN:
SELECT Faculty_Id, Faculty_First_Name, Faculty_Dept_ID Faculty_Joining_Date, Faculty_Salary FROM Faculty_Info WHERE Faculty_Joining_Date IN (2020-01-02, 2021-06-21, 2020-10-10, 2019-07-15);
Ta poizvedba prikazuje samo tiste zapise fakultet, katerih datum pridružitve je podan v operatorju IN klavzule WHERE.
oblazinjenje css
Izhod:
Faculty_Id | Ime_fakultete | Faculty_Dept_Id | Datum_vstopa na fakulteto | Fakulteta_Plača |
---|---|---|---|---|
1001 | medved | 4001 | 2020-01-02 | 20000 |
1004 | Saurabh | 4001 | 2020-10-10 | 45000 |
1005 | Shivani | 4001 | 2019-07-15 | 42000 |
1007 | Shyam | 4003 | 2021-06-21 | 35000 |
Naslednja poizvedba uporablja ukaz SQL UPDATE z logičnim operatorjem IN:
UPDATE Faculty_Info SET Faculty_Salary = 50000 WHERE Faculty_Dept_ID IN (4002, 4003);
Ta poizvedba posodobi plačo tistih fakultet, katerih Dept_Id je podan v operatorju IN klavzule WHERE.
Če želite preveriti rezultat zgornje poizvedbe, v SQL vnesite naslednjo poizvedbo SELECT:
SELECT * FROM Faculty_Info;
Faculty_Id | Ime_fakultete | Fakulteta_priimek | Faculty_Dept_Id | Datum_vstopa na fakulteto | Fakulteta_Mesto | Fakulteta_Plača |
---|---|---|---|---|---|---|
1001 | medved | Sharma | 4001 | 2020-01-02 | Delhi | 20000 |
1002 | Bulbul | Roy | 4002 | 2019-12-31 | Delhi | 50000 |
1004 | Saurabh | Roy | 4001 | 2020-10-10 | Mumbaj | 45000 |
1005 | Shivani | Singhania | 4001 | 2019-07-15 | Kolkata | 42000 |
1006 | Avinaš | Sharma | 4002 | 2019-11-11 | Delhi | 50000 |
1007 | Shyam | Ti poljubi | 4003 | 2021-06-21 | Lucknow | 50000 |
Operator SQL IN s podpoizvedbo
V Structured Query Language lahko uporabimo podpoizvedbo tudi z logičnim operatorjem IN.
Sintaksa operatorja IN s podpoizvedbo je podana spodaj:
SELECT Column_Name_1, Column_Name_2, Column_Name_3, ......, Column_Name_N FROM Table_Name WHERE Column_Name IN (Subquery);
Če želite razumeti operator IN s podpoizvedbo, morate ustvariti dve različni tabeli v jeziku strukturiranih poizvedb s stavkom CREATE.
Naslednja poizvedba ustvari tabelo Faculty_Info v zbirki podatkov:
f-string python
CREATE TABLE Faculty_Info ( Faculty_ID INT NOT NULL PRIMARY KEY, Faculty_First_Name VARCHAR (100), Faculty_Last_Name VARCHAR (100), Faculty_Dept_Id INT NOT NULL, Faculty_Address Varchar (80), Faculty_City Varchar (80), Faculty_Salary INT );
Naslednja poizvedba ustvari Oddelek_Informacije tabela v bazi podatkov:
CREATE TABLE Department_Info ( Dept_Id INT NOT NULL, Dept_Name Varchar(100), Head_Id INT );
Naslednje poizvedbe INSERT vstavijo zapise fakultet v tabelo Faculty_Info:
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_NameFaculty_Dept_Id, Faculty_Address, Faculty_City, Faculty_Salary) VALUES (1001, Arush, Sharma, 4001, 22 street, New Delhi, 20000); INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_NameFaculty_Dept_Id, Faculty_Address, Faculty_City, Faculty_Salary) VALUES (1002, Bulbul, Roy, 4002, 120 street, New Delhi, 38000 ); INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_NameFaculty_Dept_Id, Faculty_Address, Faculty_City, Faculty_Salary) VALUES (1004, Saurabh, Sharma, 4001, 221 street, Mumbai, 45000); INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_NameFaculty_Dept_Id, Faculty_Address, Faculty_City, Faculty_Salary) VALUES (1005, Shivani, Singhania, 4001, 501 street, Kolkata, 42000);
Naslednje poizvedbe INSERT vstavijo zapise oddelkov v tabelo Department_Info:
INSERT INTO Department_Info (Dept_ID, Dept_Name, Head_Id) VALUES ( 4001, Arun, 1005); INSERT INTO Department_Info (Dept_ID, Dept_Name, Head_Id) VALUES ( 4002, Zayant, 1009); INSERT INTO Department_Info (Dept_ID, Dept_Name, Head_Id) VALUES ( 4003, Manish, 1007);
Naslednji stavek SELECT prikaže podatke tabele Faculty_Info:
SELECT * FROM Faculty_Info;
Faculty_Id | Ime_fakultete | Fakulteta_priimek | Faculty_Dept_Id | Naslov_fakultete | Fakulteta_Mesto | Fakulteta_Plača |
---|---|---|---|---|---|---|
1001 | medved | Sharma | 4001 | 22 ulica | New Delhi | 20000 |
1002 | Bulbul | Roy | 4002 | 120 ulica | New Delhi | 38000 |
1004 | Saurabh | Roy | 4001 | 221 ulica | Mumbaj | 45000 |
1005 | Shivani | Singhania | 4001 | 501 ulica | Kolkata | 42000 |
1006 | Avinaš | Sharma | 4002 | 12 ulica | Delhi | 28000 |
1007 | Shyam | Ti poljubi | 4003 | 202 ulica | Lucknow | 35000 |
Naslednja poizvedba prikazuje zapise oddelka iz tabele Department_Info:
SELECT * FROM Department_Info;
Naslednja poizvedba uporablja operator IN s podpoizvedbo:
SELECT * FROM Faculty_Info WHERE Faculty_Dept_Id IN ( Select Dept_Id FROM Department_Info WHERE Head_Id >= 1007);
Ta poizvedba prikaže zapis tistih fakultet, katerih Dept_ID tabele Faculty_Info se ujema z Dept_Id tabele Department_Info.
Izhod:
Faculty_Id | Ime_fakultete | Fakulteta_priimek | Faculty_Dept_Id | Naslov_fakultete | Fakulteta_Mesto | Fakulteta_Plača |
---|---|---|---|---|---|---|
1002 | Bulbul | Roy | 4002 | 120 ulica | New Delhi | 38000 |
1006 | Avinaš | Sharma | 4002 | 12 ulica | Delhi | 28000 |
1007 | Shyam | Ti poljubi | 4003 | 202 ulica | Lucknow | 35000 |
Kaj NI IN v SQL?
NOT IN je še en operator v jeziku strukturiranih poizvedb, ki je ravno nasproten operatorju SQL IN. Omogoča vam dostop do tistih vrednosti iz tabele, ki ne preidejo v oklepaj operatorja IN.
Operator NOT IN lahko uporabite v poizvedbah SQL INSERT, UPDATE, SELECT in DELETE.
Sintaksa operatorja NOT IN:
SELECT Column_Name_1, Column_Name_2, Column_Name_3, ......, Column_Name_N FROM Table_Name WHERE Column_Name NOT IN (Value_1, Value_2, Value_3, ......., Value_N);
Če želite uporabiti operator NOT IN v stavkih SQL, morate slediti podanim korakom v istem zaporedju:
- Ustvari bazo podatkov v sistemu SQL.
- Ustvarite novo tabelo v bazi podatkov.
- Podatke vnesite v tabelo
- Oglejte si vstavljene podatke
- Za ogled podatkov uporabite operator NOT IN.
Zdaj bomo na kratko razložili vsak korak enega za drugim z najboljšim primerom SQL:
1. korak: Ustvarite preprosto novo bazo podatkov
Naslednja poizvedba ustvari novo Civilna_industrija Baza podatkov v strežniku SQL:
CREATE Database Industry;
2. korak: Ustvarite novo tabelo
Naslednja poizvedba ustvari Delavec_info tabela v Civilna_industrija Baza podatkov:
CREATE TABLE Worker_Info ( Worker_ID INT NOT NULL PRIMARY KEY, Worker_Name VARCHAR (100), Worker_Gender Varchar(20), Worker_Age INT NOT NULL DEFAULT 18, Worker_Address Varchar (80), Worker_Salary INT NOT NULL );
3. korak: Vstavite vrednosti
Naslednje poizvedbe INSERT vstavijo zapise delavcev v tabelo Worker_Info:
INSERT INTO Worker_Info (Worker_ID, Worker_Name, Worker_Gender, Worker_Age, Worker_Address, Worker_Salary) VALUES (1001, Arush, Male, Agra, 35000); INSERT INTO Worker_Info (Worker_ID, Worker_Name, Worker_Gender, Worker_Age, Worker_Address, Worker_Salary) VALUES (1002, Bulbul, Female, Lucknow, 42000); INSERT INTO Worker_Info (Worker_ID, Worker_Name, Worker_Gender, Worker_Age, Worker_Address, Worker_Salary) VALUES (1004, Saurabh, Male, 20, Lucknow, 45000); INSERT INTO Worker_Info (Worker_ID, Worker_Name, Worker_Gender, Worker_Age, Worker_Address, Worker_Salary) VALUES (1005, Shivani, Female, Agra, 28000); INSERT INTO Worker_Info (Worker_ID, Worker_Name, Worker_Gender, Worker_Age, Worker_Address, Worker_Salary) VALUES (1006, Avinash, Male, 22, Delhi, 38000); INSERT INTO Worker_Info (Worker_ID, Worker_Name, Worker_Gender, Worker_Age, Worker_Address, Worker_Salary) VALUES (1007, Shyam, Male, Banglore, 20000);
4. korak: Oglejte si podatke tabele
Naslednja poizvedba prikaže podatke tabele Worker_Info.
SELECT * FROM Worker_Info;
ID_delavca | Delavec_Name | Delavec_Spol | Delavec_Age | Naslov_delavca | Delavska_Plača |
---|---|---|---|---|---|
1001 | medved | moški | 18 | Agra | 35000 |
1002 | Bulbul | ženska | 18 | Lucknow | 42000 |
1004 | Saurabh | moški | dvajset | Lucknow | 45000 |
1005 | Shivani | ženska | 18 | Agra | 28000 |
1006 | Avinaš | moški | 22 | Delhi | 38000 |
1007 | Shyam | moški | 18 | Bangalore | 20000 |
4. korak: Uporabite operator NOT IN
Naslednja poizvedba uporablja operator NOT IN s številskimi podatki:
naključno c
SELECT * FROM Worker_Info WHERE Worker_salary NOT IN (35000, 28000, 38000);
Ta poizvedba SELECT prikazuje vse tiste delavce v izhodu, katerih plača ni posredovana v operatorju NOT IN.
Rezultat zgornje izjave je prikazan v naslednji tabeli:
ID_delavca | Delavec_Name | Delavec_Spol | Delavec_Age | Naslov_delavca | Delavska_Plača |
---|---|---|---|---|---|
1002 | Bulbul | ženska | 18 | Lucknow | 42000 |
1004 | Saurabh | moški | dvajset | Lucknow | 45000 |
1007 | Shyam | moški | 18 | Bangalore | 20000 |
Naslednja poizvedba uporablja logični operator NOT IN z vrednostmi znakov ali besedila:
SELECT * FROM Worker_Info WHERE Worker_Address NOT IN (Lucknow, Delhi);
Ta poizvedba prikazuje zapis vseh delavcev, katerih naslov ni posredovan v operatorju NOT IN.
Izhod:
ID_delavca | Delavec_Name | Delavec_Spol | Delavec_Age | Naslov_delavca | Delavska_Plača |
---|---|---|---|---|---|
1001 | medved | moški | 18 | Agra | 35000 |
1005 | Shivani | ženska | 18 | Agra | 28000 |
1007 | Shyam | moški | 18 | Bangalore | 20000 |