已编辑
试试这个:
SELECT * FROM complaints
WHERE NOT EXISTS
(SELECT complaintid
FROM tasks
WHERE complaintid
in (SELECT complaintID
FROM tasks
WHERE tasks.complaintID = complaints.complaintid
and completed = 'no')
)ORDER BY deadline;
SQL 小提琴演示
这是用于测试我上面的答案的表结构和示例数据。
CREATE TABLE complaints (
complaintID INT AUTO_INCREMENT PRIMARY KEY,
content VARCHAR(255),
deadline datetime
);
CREATE TABLE tasks (
tasksid INT AUTO_INCREMENT PRIMARY KEY,
complaintID INT,
completed ENUM ('yes','no')
);
INSERT INTO complaints(content,deadline) VALUES ('complaint1', NOW()+INTERVAL 3 day), ('complaint2',NOW()+INTERVAL 8 day),('complaint3', NOW()+INTERVAL 3 day);
INSERT INTO tasks (complaintID, completed) VALUES(1,'no'), (1,'yes'), (2,'yes'), (1,'no'), (1,'yes'), (1,'yes'), (2,'no'), (3,'yes'), (3, 'yes');