简体   繁体   English

使用MySQL限制每天发送的访问者

[英]Using MySQL to Limit Visitors Sent Per Day

I'm setting up a website that uses PHP and MySQLi. 我正在建立一个使用PHP和MySQLi的网站。 The website sells traffic to other website owners. 该网站将流量出售给其他网站所有者。 Here's the structure of my table that monitors active campaigns: 这是我监视活动广告系列的表格的结构:

id  url                     hits_ordered    hits_sent   status
1   http://google.com       1000            120         'Active'
2   http://facebook.com     3000            2421        'Inactive'
3   http://yahoo.com        400             12          'Active'

What I want to do is give people the option to have hits spread across 1-90 days, that way they don't just get flooded with them all at once. 我想做的是让人们可以选择在1-90天内传播点击量,这样一来,他们就不会立即被全部淹没。 So, I was going to change the structure to this: 因此,我将结构更改为:

id  url                     hits_ordered    hits_sent   period      status
1   http://google.com       1000            120         25          'Active'
2   http://facebook.com     3000            2421        20          'Inactive'
3   http://yahoo.com        400             12          60          'Active'

The column period would represent the number of days the campaign should be spread across. period表示广告系列应分散的天数。

But I'm thinking that it may be better to add two columns instead, one that would show the time the campaign started ( start_time ) and when the campaign should be completed ( end_time ). 但是我认为最好添加两列,一列显示活动开始的时间( start_time )和活动应该何时结束( end_time )。

If I had start and end columns, would there be any way with MySQL to mathematically calculate if a campaign was behind schedule and select it? 如果我有开始和结束列,那么MySQL是否可以通过数学方法计算出广告活动是否落后于计划并选择了它?

For example, if someone order 10,000 hits and wanted them delivered over the course of 5 days. 例如,如果某人订购了10,000个匹配,并希望他们在5天内交付。 If on the 4th day, they only have 2000 hits, obviously it would be "behind schedule" because it should have between 8000-10000 hits. 如果在第4天,他们只有2000次点击,显然这将“落后于计划”,因为它应该具有8000-10000次点击。

I know that there's ways I could calculate this with PHP. 我知道我可以使用PHP来计算。 I'm just trying to figure out if there would be a way to do this with purely MySQL to select all rows that are behind schedule. 我只是想弄清楚是否有一种方法可以使用纯MySQL来选择所有落后于计划的行。

It seems like there should be a way to query these rows since MySQL can perform math, knows the total hits sent, the total amount order, and the start/end dates. 似乎应该有一种查询这些行的方法,因为MySQL可以执行数学运算,知道发送的总点击数,总金额顺序以及开始/结束日期。

================ ================

Another example for @ICE below. 下面是@ICE的另一个示例。 This row would be behind schedule: 该行将落后于计划:

id  url                     hits_ordered    hits_sent   start_date              end_date
1   http://google.com       1000            5           2016-12-23 00:00:00     2016-12-28 00:00:00

It's behind because only 5 hits have been sent since it was ordered on the 23rd. 落后是因为自23日订购以来仅发送了5次匹配。 It's now the 25th and ends on the 28th. 现在是25日,到28日结束。 So according to my logic above, it should be considered "behind schedule". 因此,根据我上面的逻辑,应将其视为“落后于进度”。

And this row would not be: 并且此行不会是:

id  url                     hits_ordered    hits_sent   start_date              end_date
1   http://google.com       1000            999         2016-12-23 00:00:00     2016-12-28 00:00:00

In this example, the dates are the same, but 999 our of 1000 hits have been sent. 在此示例中,日期相同,但是已发送1000个匹配中的999个。 It would not be behind schedule. 这不会落后于时间表。

No need for column period . 无需专栏period Create end_time and start_time as DATETIME . 创建end_timestart_time作为DATETIME after that you can select them with datediff . 之后,您可以使用datediff选择它们。 It returns difference between two dates in days: 它返回两个日期之间的天数差异:

select * from TABLE where
datediff(end_time,start_time) > datediff(now(),start_time) and
datediff(now(),start_time)>3 and //just select rows with more than 3 days after start_time. This can help to have more logical result as you want
hits_sent + (datediff(end_time,now()) * (hits_send/datediff(now(),start_time))) + 20 < hits_ordered //means count hits happened + we guess this much hits can happen on remaining days based on the remaining days and hits_send + fluctuation < what they ordered

Explaining 讲解

hits per day until now 直到现在每天点击

hits_send/datediff(now(),start_time)

remaining days 剩余的日子

datediff(end_time,now())

With what happened in days before now we can guess this much hits could happen in remaining days 根据现在几天前发生的情况,我们可以猜测剩下的日子可能会发生如此多的打击

(datediff(end_time,now()) * (hits_send/datediff(now(),start_time))) + 20

+20 is the fluctuation. +20是波动。 We guess maybe some day they can have 20 hits more. 我们猜想也许有一天他们还会有20首热门歌曲。

You can compare the percentage of the remaining hits and the percentage of days of the campain, if the percentage of the hits_sent is above the percentage of the campain duration percentage, just disable it with a bool expresion. 如果hits_sent的百分比高于campain持续时间百分比,则可以比较剩余匹配的百分比和campain的天百分比,只需使用布尔表达式禁用它即可。

select *,((hits_sent/hits_ordered)<datediff(now(),start_date)/datediff(end_date,start_date)) as active, (((hits_sent/hits_ordered))/(datediff(now(),start_date)/datediff(end_date,start_date)) ) as status from orders where 1 order by active desc

In a SQLFIDDLE gives the active or not campain, and give you a number that represent if the campain is on schedule (near 1) below schedule (near 0) and above 1, when its already complete SQLFIDDLE中,给出活动的或没有活动的战役,并给您一个数字,该数字表示战役是否已按计划(接近1)低于时间表(接近0)且高于1(如果已经完成)

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

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