2016-05-04 29 views
2

我在不同的日期有一个鱼cought表。Oracle在最短日期列表中的日期

表着陆:

Long ID, 
Long Vessel_id, -- vessel that is fishing 
TimeStamp fishing_date, 
double amount_cought 
Long species_id; 

接下来是使用船只鱼公司的表。

表ves_usage:

Long ID, 
TimeStamp usage_start_date, 
TimeStamp usage_end_date, 
Long vessel_id, 
Long using_company_id; 

最后我有执照的表中某一年以鱼为一家公司的具体种类。

表许可证:

Long ID, 
int year, --the year that the lincence is active 
Long licenced_company_id,--company who was given the licence 
Long species_id; --species that the company can fish 

我想选择所有从着陆表中有一个特定的物种,公司和船舶的记录。我遇到的问题是我不能想到一种方法来选择ves_usage表记录的开始日期和结束日期之间的所有着陆记录。不确定是不是我不够清楚:P 我想要的东西,如:

Select * 
from landings 
where fishing_date between <list of usage_start_date and usage_end_date from ves_usage table> 
+2

你需要做一个连接。 (或者有很多的或者是BETWEEN。) – jarlh

+5

样本数据,尤其是期望的结果会让你清楚你想要完成什么。 –

+0

那么你想要一个特定的公司(使用'using_company_id')完成的所有着陆或在特定旅程中完成的所有着陆(使用'ves_usage'表中的'id')吗?此外,你给了我们'licences'表;你有什么想做的吗? –

回答

0

如果我下面的表格正确,我相信查询会是这样的:

SELECT l.* 
FROM landings l 
    JOIN ves_usage vu 
      ON vu.vessel_id = l.Vessel_id 
      AND l.fishing_date BETWEEN vu.usage_start_date AND vu.usage_end_date 
    JOIN licences li 
      ON li.vessel_id = vu.Vessel_id 
      AND li.species = l.species_id 
WHERE l.fishing_date = /*date here*/ 
     AND li.licenced_company_id = /*company id here*/ 
+0

查询结束了这样的事情。只是为了一些规则,一个常规的JOIN提取所有数据。当我将连接移到FROM ... WHERE时,数据被正确选择 – mindz

0

如果您想查询是基于IDves_usage,这个查询将工作:

SELECT * 
FROM landings 
WHERE fishing_date BETWEEN 
    (SELECT usage_start_date FROM ves_usage WHERE id = <id from ves_usage>) 
    AND 
    (SELECT usage_end_date FROM ves_usage WHERE id = <id from ves_usage>) 

如果您想要不同的东西,请按照评论中的要求指定。