SQL EXISTS Operator - W3Schools
Có thể bạn quan tâm
The SQL EXISTS Operator
The EXISTS operator is used to test for the existence of any record in a subquery.
The EXISTS operator returns TRUE if the subquery returns one or more records.
EXISTS Syntax
SELECT column_name(s) FROM table_name WHERE EXISTS(SELECT column_name FROM table_name WHERE condition);Demo Database
Below is a selection from the "Products" table in the Northwind sample database:
| ProductID | ProductName | SupplierID | CategoryID | Unit | Price |
|---|---|---|---|---|---|
| 1 | Chais | 1 | 1 | 10 boxes x 20 bags | 18 |
| 2 | Chang | 1 | 1 | 24 - 12 oz bottles | 19 |
| 3 | Aniseed Syrup | 1 | 2 | 12 - 550 ml bottles | 10 |
| 4 | Chef Anton's Cajun Seasoning | 2 | 2 | 48 - 6 oz jars | 22 |
| 5 | Chef Anton's Gumbo Mix | 2 | 2 | 36 boxes | 21.35 |
And a selection from the "Suppliers" table:
| SupplierID | SupplierName | ContactName | Address | City | PostalCode | Country |
|---|---|---|---|---|---|---|
| 1 | Exotic Liquid | Charlotte Cooper | 49 Gilbert St. | London | EC1 4SD | UK |
| 2 | New Orleans Cajun Delights | Shelley Burke | P.O. Box 78934 | New Orleans | 70117 | USA |
| 3 | Grandma Kelly's Homestead | Regina Murphy | 707 Oxford Rd. | Ann Arbor | 48104 | USA |
| 4 | Tokyo Traders | Yoshi Nagase | 9-8 Sekimai Musashino-shi | Tokyo | 100 | Japan |
SQL EXISTS Examples
The following SQL statement returns TRUE and lists the suppliers with a product price less than 20:
Example
SELECT SupplierNameFROM SuppliersWHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID = Suppliers.supplierID AND Price < 20); Try it Yourself »The following SQL statement returns TRUE and lists the suppliers with a product price equal to 22:
Example
SELECT SupplierNameFROM SuppliersWHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID = Suppliers.supplierID AND Price = 22); Try it Yourself » ❮ Previous Next ❯ ★ +1 Sign in to track progressTừ khóa » Câu Lệnh If Exists Trong Sql
-
Điều Kiện EXISTS Trong SQL Server
-
Toán Tử EXISTS Trong SQL Server - Comdy
-
Toán Tử EXISTS Trong SQL - BAC
-
EXISTS Kiểm Tra Sự Tồn Tại Trong SQL - Deft Blog
-
[DATABASE] Sử Dụng Câu Lệnh DROP IF EXISTS Trong Sqlserver ...
-
SQL Server: EXISTS Và NOT EXISTS - V1Study
-
Lệnh EXISTS Trong MySQL - Web Cơ Bản
-
Tổng Quan Về Câu Lệnh T-SQL If Exists Trong Cơ Sở Dữ Liệu SQL Server
-
Làm Thế Nào để Sử Dụng DROP NẾU TỒN TẠI Trong SQL Server?
-
Cách Sử Dụng "IF EXISTS" Của MySQL
-
Làm Cách Nào để Sử Dụng Câu Lệnh SQL Chọn Với Truy Vấn Phụ IF ...
-
Cách Sử Dụng BIẾN, IF ... ELSE Trong SQL Server Thật đơn Giản
-
Lệnh Rẻ Nhánh IF ELSE Trong SQL Server - Freetuts - MarvelVietnam
-
Điều Kiện EXISTS Trong SQL Server - Phần Mềm FREE