2016-05-04 30 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>) 

如果您想要不同的東西,請按照評論中的要求指定。