我有訂閱表,其中包括與屏幕上的所有訂閱PF廣告。我想限制用戶每次只能爲每個屏幕添加20個廣告。我已完成每個屏幕的廣告計數。但問題是,我想考慮日期範圍(每個訂閱的開始日期和結束日期)。這意味着廣告在特定日期範圍內不應超過20次。計數MySQL表字段
我已經建立樣本模式的sqlfiddle。 http://sqlfiddle.com/#!2/070cf/1
幫助將不勝感激。謝謝。
我有訂閱表,其中包括與屏幕上的所有訂閱PF廣告。我想限制用戶每次只能爲每個屏幕添加20個廣告。我已完成每個屏幕的廣告計數。但問題是,我想考慮日期範圍(每個訂閱的開始日期和結束日期)。這意味着廣告在特定日期範圍內不應超過20次。計數MySQL表字段
我已經建立樣本模式的sqlfiddle。 http://sqlfiddle.com/#!2/070cf/1
幫助將不勝感激。謝謝。
如果開始和結束日期都投入到你的SELECT語句:
SELECT count(screen_id) as ad_count
FROM subscription
WHERE
screen_id=1 AND
DATE_START >= STR_TO_DATE('19,01,2014','%d,%m,%Y') and
DATE_END <= STR_TO_DATE('23,01,2014','%d,%m,%Y');
不,這是動態的。它已經在數據庫表中。檢查sqlfiddle鏈接。 –
您對此查詢的輸入是什麼以及預期的結果是什麼?你想檢查是否有代理人超過這個限制? –
是的。我需要檢查,在任何代理添加新訂閱之前,開始和結束日期範圍內的總訂閱數不超過20。 –
你需要檢查的條件爲每天的時間間隔。這是我強烈建議在應用程序層進行的事情,因爲mysql實際上不是爲此循環而構建的。
但對於參數的緣故,這裏是如何解決它的SQL: 1.展開搜索間隔所有的日子 2.每天檢查條件
第1步:擴大間隔
select *, start_date+interval nr.number day as search_date -- search date is the exploded interval
from
(select date('2013-01-13') as start_date, date('2013-02-20') as end_date) search -- the dates we search for
inner join -- joining a numbers table to expand the interval
(select a.nr+b.nr*10 as number from
(select 1 as nr
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9
union all select 0
)a
join
(select 1 as nr
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9
union all select 0
)b
order by 1 asc
) nr
on nr.number<=datediff(end_date, start_date)
現在你有列日期,結束日期,search_date,其中搜索日期包含開始和結束之間的所有日期。讓我們把這個查詢「DATE_INTERVAL」
假設您有與其他DATE_START和DATE_END一個表,按您的訂閱表。要計算如果你每天不超過20個活動條件得到滿足,andto找到有問題的日子裏,我們一起像這樣:
select search_date, count(*) as subscriptions_active from
date_interval as di
inner join
subscriptions as s
on di.search_date between s.date_start and s.date_end
group by search_date
having subscriptions_active <20
導致從我們的搜索間隔20個或更多的活動日期表。
另一種方法是檢查運行總計,whcih將與日期時間的工作。 假設具有start_date和end_date的查詢,查詢示例如下。現在可以很容易地檢查在該表中搜索到的開始和結束日期之間是否存在不符合條件的日期時間。
select c.date, c.change_,
(select sum(change_) from (select start_date as date, change_ from
(select start_date , 1 as change_ from
(select date('2013-01-13') as start_date, date('2013-02-20') as end_date
union all
select date('2013-01-14') as start_date, date('2013-04-25') as end_date
union all
select date('2013-03-15') as start_date, date('2013-05-25') as end_date)a
union
select end_date, -1 as change_ from
(select date('2013-01-13') as start_date, date('2013-02-20') as end_date
union all
select date('2013-01-14') as start_date, date('2013-04-25') as end_date
union all
select date('2013-03-15') as start_date, date('2013-05-12') as end_date)a
order by 1 asc)b
)d where d.date<=c.date) as running_total
from
(select start_date as date, change_ from
(select start_date , 1 as change_ from
(select date('2013-01-13') as start_date, date('2013-02-20') as end_date
union all
select date('2013-01-14') as start_date, date('2013-04-25') as end_date
union all
select date('2013-03-15') as start_date, date('2013-05-25') as end_date)a
union
select end_date, -1 as change_ from
(select date('2013-01-13') as start_date, date('2013-02-20') as end_date
union all
select date('2013-01-14') as start_date, date('2013-04-25') as end_date
union all
select date('2013-03-15') as start_date, date('2013-05-12') as end_date)a
order by 1 asc)b
)c
你的問題是有點不精確。你試圖限制數量到什麼範圍?同時,探索「GROUP BY」和可能的「HAVING」條款。 – Palpatim
是的。每個訂閱都有一個開始日期和結束日期。在此範圍內,廣告應小於20. –
那麼您是否試圖在數據庫中輸入任何條目之前,在期間/用戶的數據庫中是否已存在最大條目數? –