Categories SQL

Toán tử IN trong SQL

Toán tử IN cho phép bạn chỉ định nhiều giá trị trong mệnh đề WHERE.

Cú pháp SQL IN:

SELECT ten_cot (có thể nhiều cột)
FROM ten_bang
WHERE ten_cot IN (gia_tri_1,gia_tri_2,…);

Cơ sở dữ liệu Demo

Trong bài viết này chúng ta sẽ sử dụng cơ sở dữ liệu mẫu Northwind được nhiều người biết đến.

Dưới đây là một lựa chọn từ bảng “Customers”:

CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden

Ví dụ về toán tử IN

Câu lệnh SQL sau lựa chọn tất cả các khách hàng với City là “Paris” hoặc “London”:

SELECT * FROM Customers
WHERE City IN ('Paris','London');

Kết quả (lấy ra hai ví dụ):

57 Paris spécialités Marie Bertrand 265, boulevard Charonne Paris 75012 France
72 Seven Seas Imports Hari Kumar 90 Wadhurst Rd. London OX15 4NB UK
Back to Top