简体   繁体   English

MySQL子查询和'NOT IN'

[英]MySQL sub-query AND 'NOT IN'

sorry if this has been answered before; 抱歉,如果以前已经回答过; I'm a little unsure how best to describe this problem never mind search for it. 我有点不确定如何最好地描述这个问题,不要介意寻找它。 But here goes... 但是这里...

Basically I have a 'projects' table, which holds and 'id' and a 'title'. 基本上,我有一个“项目”表,其中包含“ id”和“ title”。 I also have a 'projects_history' table which holds information when (IF) the project is set to archived (a boolean value). 我还有一个'projects_history'表,该表保存(IF)将项目设置为已归档(布尔值)时的信息。 It has a 'pid' key that references the project - there can be more than one record for each project as it is updated (in order to track who sets the value to what). 它有一个引用该项目的“ pid”键-每个项目在更新时可以有多个记录(以便跟踪谁将值设置为什么)。

There's also a 'project_enquiries' table that holds information on enquiries that have been raised for the project, so there is a 'pid' key that references 'projects'. 还有一个“ project_enquiries”表,其中包含有关已针对项目提出的查询的信息,因此有一个“ pid”键引用了“ projects”。 Similarly, there's a 'project_enquiry_history' table that records when (IF) the enquiry is set to closed (a boolean value). 同样,有一个“ project_enquiry_history”表记录(IF)何时将查询设置为关闭(布尔值)。 It has a 'eid' key that references the project_enquiry - there can be more than one record for each enquiry as it is updated (in order to track who sets the value to what). 它具有引用project_enquiry的“ eid”键-每个查询在更新时都可以有多个记录(以便跟踪谁将值设置为什么)。

My query aims to pull out the projects that haven't been archived (so either there is no record in 'project_history' or the most recent record for the project has 'archived' = 0), which have enquiries that are still open (so either there is no record in 'project_enquiry_history' or the most recent record for the enquiry has 'open' = 1). 我的查询旨在提取尚未归档的项目(因此,“ project_history”中没有记录,或者该项目的最新记录具有“ archived” = 0),这些项目的查询仍处于打开状态(因此“ project_enquiry_history”中没有记录,或者查询的最新记录中“ open” = 1)。

I'm really struggling on where to start with the query. 我真的很难从哪里开始查询。

The first thing is that if there is no record in project_history , then you will need to do a left join of your projects table on the project_history table using your project id to discover this. 第一件事是,如果project_history没有记录,那么您将需要使用您的项目ID在project_history表上对projects表进行left join ,以发现这一点。 You will have entries in the results that are null in the columns for project_history where there is no corresponding entry. 结果中的条目将在project_history的列中为空,其中没有相应的条目。 You can then chain that process, using the result to do another left join on the 'project_enquiry_history' to find the items that have no entries. 然后,您可以链接该过程,使用结果对'project_enquiry_history'进行另一个左联接,以查找没有条目的项目。

You can also use these results as normal, to do selects of the data. 您也可以照常使用这些结果来选择数据。

It will look something like: 它看起来像:

SELECT * from project p LEFT OUTER JOIN project_history ph ON P.ID=ph.pid LEFT OUTER JOIN project_enquiry_history peh ON peh.ID=ph.pid WHERE (archived is NULL OR archived=0) AND (open is NULL OR open=1)

You may have to tweak this to find the most recent if you are using timestamps and such. 如果使用时间戳等,则可能必须进行调整以找到最新的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM