Subquery con EXISTS
Sintassi
SELECT ... WHERE EXISTS <subquery_tabella>
Spiegazione
Le subquery che usano la parola chiave EXISTS
restituiscono TRUE
se la subquery restituisce almeno una riga. Al contrario, le subquery che usano NOT EXISTS
restituiscono TRUE
solo se la subquery non restituisce alcuna riga.
Le subquery con EXISTS ignorano le colonne specificate nella parte SELECT della subquery, perché non sono rilevanti. Per esempio,
SELECT col1 FROM t1 WHERE EXISTS (SELECT * FROM t2);
e
SELECT col1 FROM t1 WHERE EXISTS (SELECT col2 FROM t2);
producono risultati identici.
Esempi
CREATE TABLE sq1 (num TINYINT); CREATE TABLE sq2 (num2 TINYINT); INSERT INTO sq1 VALUES(100); INSERT INTO sq2 VALUES(40),(50),(60); SELECT * FROM sq1 WHERE EXISTS (SELECT * FROM sq2 WHERE num2>50); +------+ | num | +------+ | 100 | +------+ SELECT * FROM sq1 WHERE NOT EXISTS (SELECT * FROM sq2 GROUP BY num2 HAVING MIN(num2)=40); Empty set (0.00 sec)
Content reproduced on this site is the property of its respective owners,
and this content is not reviewed in advance by MariaDB. The views, information and opinions
expressed by this content do not necessarily represent those of MariaDB or any other party.