SQL 条件语句

SQL Condition statement

我正在使用 MySQL 5.6.23。在我的数据库中,我有两个 tables,tblFiles 和 tblLogs。 table tblFiles 包含 ID (PK)、文件路径和文件名(均为 VARCHAR)。

table tblLogs 包含一个 ID (PK)、一个指向 tblFiles 中条目的引用 ID (FK) 和一个 actionCode(0、1 或 2)。

tblFiles 中的条目可能如下所示:

1 | c:\root\files | test1.txt
2 | c:\root\files | test2.txt
3 | c:\root\files | test3.txt

tblFiles 中的条目可能看起来像

1 | 1 | 0
2 | 1 | 1
3 | 2 | 0
4 | 3 | 2

我需要为所有在 tblLogs 中只有 actionCode 0 的文件获取路径和文件名(来自 tblFiles)。对于上面的示例,查询应该 return:

c:\root\files | test2.txt

如果您觉得这个问题很奇怪,那是因为这是我的作业的一部分。现在,我正在尝试弄清楚如何获取只有 actionCode 0 的条目(即只获取一个 return me referenceID 2 的查询)。我尝试了不同的查询,例如:

SELECT referenceID FROM tblLogs WHERE actionCode = 0 AND (actionCode != 1 OR actionCode != 2);

我什至不知道这样的查询是否可行...如果你们能帮助我,我将不胜感激!

这是使用 maxmin 的一种方法:

select f.loc, f.name
from tblFiles f
  join tblLogs l on f.id = l.referenceid
group by f.loc, f.name
having max(l.actioncode) = 0 and min(l.actioncode) = 0