Kết hợp hàm IF(condition, true_value, false_value) với toán tử OR trong điều kiện.
Ví dụ bảng EmployeeInformation:
CREATE TABLE EmployeeInformation (
EmployeeId INT, EmployeeName VARCHAR(100), EmployeeStatus VARCHAR(100)
);
INSERT INTO EmployeeInformation VALUES
(1,'Sam','FullTime'), (2,'Mike','PartTime'), (3,'Bob','Intern'),
(4,'Carol','FullTime'), (5,'John','FullTime'), (6,'Johnson','PartTime'), (7,'Maria','Intern');
Yêu cầu: Nếu trạng thái là 'FullTime' HOẶC 'Intern' → hiển thị tên, ngược lại hiển thị trạng thái.
SELECT IF(EmployeeStatus = 'FullTime' OR EmployeeStatus = 'Intern',
EmployeeName, EmployeeStatus) AS Status
FROM EmployeeInformation;
Kết quả
+----------+ | Status | +----------+ | Sam | | PartTime | | Bob | | Carol | | John | | PartTime | | Maria | +----------+
Mở rộng: Dùng CASE WHEN ... THEN ... ELSE ... END cho logic phức tạp hơn nhiều điều kiện.