我试图获取所有可用的密钥,这意味着已经返回的密钥(return_date 不为空),密钥没有丢失(lost_key = false),并且之前从未租用过密钥(id 为空)。但是,当我运行它时,我得到 0 个记录。如果我删除 id 条件,我只会得到之前租用的密钥,并且所有不在 keyActivity 表中的密钥都将被忽略。有谁知道我做错了什么?
SELECT a.key_id, a.drawer_num
FROM
keys AS a
left outer JOIN keyActivity AS b
ON a.key_id = b.key_id
WHERE
return_date is not null
and lost_key =false
and id is null;
使用的解决方案:
SELECT a.key_id, a.drawer_num
FROM keys AS a LEFT JOIN keyActivity AS b ON a.key_id = b.key_id
WHERE (b.return_date is not null
and b.lost_key =false)
OR b.id is null;