0
我有一個包含數字和日期的表格(1個數字,每個日期和日期不一定是固定的時間間隔)。 我想知道某個數字不在表格中時的日期數。使用子查詢結果
我在哪裏:
select *
from
(
select
date from nums
where chiffre=1
order by date desc
limit 2
) as f
我得到這個:
基本上,我有動態此查詢:
select * from nums where date between "2014-07-26" and "2014-09-07"
,並在第二時間,瀏覽整個表(因爲那裏我只限於前2行,但我會比較2和3以及3和4等)。
的目標是得到這樣的:
date | actual_number_of_real_dates_between_two_given_dates
2014-09-07 - 2014-07-26 | 20
2014-04-02 - 2014-02-12 | 13
等等
我怎樣才能做到這一點?謝謝。
編輯:
我有什麼(只是舉個例子,日期和 「CHIFFRE」 比較複雜):
date | chiffre
2014-09-30 | 2
2014-09-29 | 1
2014-09-28 | 2
2014-09-27 | 2
2014-09-26 | 1
2014-09-25 | 2
2014-09-24 | 2
等等
我需要多少什麼?「 1" :
actual_number_of_real_dates_between_two_given_dates
1
3
等等
編輯2:
我更新的查詢感謝戈登·利諾夫
select count(n.id) as difference
from nums n inner join
(select min(date) as d1, max(date) as d2
from (select date from nums where chiffre=1 order by date desc limit 2) d
) dd
where n.date between dd.d1 and dd.d2
如何測試第2排3? 3與4等...不僅是最後2? 我應該使用循環嗎?或者我可以沒有?
肯定的,但它缺少動態部分 我想動態生成2014年7月26日和2014- 09-07 謝謝 PS:我只需要「numDates」列;) – sam12 2014-10-27 11:20:27
@ sam12。 。 。只需將你想要的日期添加到子查詢中即可。你甚至可以使用'union all'並添加多對日期。 – 2014-10-27 11:34:53
是的,但我不瀏覽整個表格。我只瀏覽了最後2個日期(限制2) – sam12 2014-10-27 11:55:40