Toán tử logic trong SQL
Bảng SINHVIEN có các bản ghi sau:
SQL> SELECT * FROM SINHVIEN; +----+---------+-----+-----------+--------+ | ID | TEN | TUOI| KHOAHOC | HOCPHI | +----+---------+-----+-----------+---------+ | 1 | Hoang | 21 | CNTT | 4 | | 2 | Viet | 19 | DTVT | 3.0 | | 3 | Thanh | 18 | KTDN | 4 | | 4 | Nhan | 19 | CK | 4.5 | | 5 | Huong | 20 | TCNH | 5 | +----+---------+-----+-----------+---------+ 5 rows in set (0.00 sec)
Dưới đây là các ví dụ minh họa cách sử dụng của các toán tử logic trong SQL:
SQL> SELECT * FROM SINHVIEN WHERE TUOI >= 19 AND HOCPHI >= 4; +----+----------+-----+---------+---------+ | ID | TEN | TUOI | KHOAHOC | HOCPHI | +----+----------+-----+---------+---------+ | 1 | Hoang | 21 | CNTT | 4 | | 4 | Nhan | 19 | CK | 4.5 | | 5 | Huong | 20 | TCNH | 5 | +----+----------+-----+---------+---------+ 3 rows in set (0.00 sec) SQL> SELECT * FROM SINHVIEN WHERE TUOI >= 19 OR HOCPHI >= 4; +----+----------+-----+-----------+----------+ | ID | TEN | TUOI | KHOAHOC | HOCPHI | +----+----------+-----+-----------+----------+ | 1 | Hoang | 21 | CNTT | 4 | | 3 | Thanh | 18 | KTDN | 4 | | 4 | Nhan | 19 | CK | 4.5 | | 5 | Huong | 20 | TCNH | 5 | +----+----------+-----+-----------+----------+ 4 rows in set (0.00 sec)SQL> SELECT * FROM SINHVIEN WHERE TUOI IS NOT NULL; +----+----------+-----+-----------+----------+ | ID | TEN | TUOI | KHOAHOC | HOCPHI | +----+----------+-----+-----------+----------+ | 1 | Hoang | 21 | CNTT | 4 | | 2 | Viet | 19 | DTVT | 3.0 | | 3 | Thanh | 18 | KTDN | 4 | | 4 | Nhan | 19 | CK | 4.5 | | 5 | Huong | 20 | TCNH | 5 | +----+----------+-----+-----------+----------+ 5 rows in set (0.00 sec)SQL> SELECT * FROM SINHVIEN WHERE TEN LIKE 'Hu%'; +----+-------+-----+---------+---------+ | ID | TEN | TUOI | KHOAHOC | HOCPHI | +----+-------+-----+---------+---------+ | 5 | Huong | 20 | TCNH | 5 | +----+-------+-----+---------+---------+ 1 row in set (0.00 sec)SQL> SELECT * FROM SINHVIEN WHERE TUOI IN ( 20, 21 ); +----+----------+-----+---------+---------+ | ID | TEN | TUOI | KHOAHOC | HOCPHI | +----+----------+-----+---------+---------+ | 1 | Hoang | 21 | CNTT | 4 | | 5 | Huong | 20 | TCNH | 5 | +----+----------+-----+---------+---------+ 2 rows in set (0.00 sec)SQL> SELECT * FROM SINHVIEN WHERE TUOI BETWEEN 20 AND 21; +----+----------+-----+---------+---------+ | ID | TEN | TUOI | KHOAHOC | HOCPHI | +----+----------+-----+---------+---------+ | 1 | Hoang | 21 | CNTT | 4 | | 5 | Huong | 20 | TCNH | 5 | +----+----------+-----+---------+---------+ 2 rows in set (0.00 sec)
toan_tu_trong_sql.jsp
Quảng cáo
Bài viết liên quan