Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.
Condizioni logiche
Le condizioni logiche combinano il risultato di due condizioni per produrre un singolo risultato. Tutte le condizioni logiche sono operatori binari con un tipo restituito booleano.
Sintassi
expression { AND | OR } expression NOT expression
Le condizioni logiche usano una logica booleana a tre valori in cui il valore null rappresenta una relazione sconosciuta. Nella tabella riportata di seguito sono descritti i risultati delle condizioni logiche, dove E1
ed E2
rappresentano espressioni:
E1 | E2 | E1 ed E2 | E1 o E2 | NO E2 |
---|---|---|---|---|
TRUE | TRUE | TRUE | TRUE | FALSE |
TRUE | FALSE | FALSE | TRUE | TRUE |
TRUE | UNKNOWN | UNKNOWN | TRUE | UNKNOWN |
FALSE | TRUE | FALSE | TRUE | |
FALSE | FALSE | FALSE | FALSE | |
FALSE | UNKNOWN | FALSE | UNKNOWN | |
UNKNOWN | TRUE | UNKNOWN | TRUE | |
UNKNOWN | FALSE | FALSE | UNKNOWN | |
UNKNOWN | UNKNOWN | UNKNOWN | UNKNOWN |
L'operatore NOT viene valutato prima di AND e l'operatore AND viene valutato prima dell'operatore OR. Eventuali parentesi utilizzate potrebbero sostituire questo ordine di valutazione predefinito.
Esempi
L'esempio seguente restituisce USERID e USERNAME dalla tabella USERS se agli utenti piacciono sia Las Vegas sia gli sport:
select userid, username from users
where likevegas = 1 and likesports = 1
order by userid;
userid | username
--------+----------
1 | JSG99FHE
67 | TWU10MZT
87 | DUF19VXU
92 | HYP36WEQ
109 | FPL38HZK
120 | DMJ24GUZ
123 | QZR22XGQ
130 | ZQC82ALK
133 | LBN45WCH
144 | UCX04JKN
165 | TEY68OEB
169 | AYQ83HGO
184 | TVX65AZX
...
(2128 rows)
L'esempio successivo restituisce USERID e USERNAME dalla tabella USERS se agli utenti piacciono Las Vegas o gli sport o entrambi. Questa query restituisce tutti gli output dell'esempio precedente più gli utenti a cui piacciono solo Las Vegas o gli sport.
select userid, username from users
where likevegas = 1 or likesports = 1
order by userid;
userid | username
--------+----------
1 | JSG99FHE
2 | PGL08LJI
3 | IFT66TXU
5 | AEB55QTM
6 | NDQ15VBM
9 | MSD36KVR
10 | WKW41AIW
13 | QTF33MCG
15 | OWU78MTR
16 | ZMG93CDD
22 | RHT62AGI
27 | KOY02CVE
29 | HUH27PKK
...
(18968 rows)
La query seguente usa le parentesi intorno alla condizione OR
per trovare i luoghi a New York o in California in cui è stato rappresentato Macbeth:
select distinct venuename, venuecity
from venue join event on venue.venueid=event.venueid
where (venuestate = 'NY' or venuestate = 'CA') and eventname='Macbeth'
order by 2,1;
venuename | venuecity
----------------------------------------+---------------
Geffen Playhouse | Los Angeles
Greek Theatre | Los Angeles
Royce Hall | Los Angeles
American Airlines Theatre | New York City
August Wilson Theatre | New York City
Belasco Theatre | New York City
Bernard B. Jacobs Theatre | New York City
...
La rimozione delle parentesi in questo esempio modifica la logica e i risultati della query.
L'esempio seguente utilizza l'operatore NOT
:
select * from category
where not catid=1
order by 1;
catid | catgroup | catname | catdesc
-------+----------+-----------+--------------------------------------------
2 | Sports | NHL | National Hockey League
3 | Sports | NFL | National Football League
4 | Sports | NBA | National Basketball Association
5 | Sports | MLS | Major League Soccer
...
L'esempio seguente utilizza una condizione NOT
seguita da una condizione AND
:
select * from category
where (not catid=1) and catgroup='Sports'
order by catid;
catid | catgroup | catname | catdesc
-------+----------+---------+---------------------------------
2 | Sports | NHL | National Hockey League
3 | Sports | NFL | National Football League
4 | Sports | NBA | National Basketball Association
5 | Sports | MLS | Major League Soccer
(4 rows)