【问题标题】:Using MySQL to Limit Visitors Sent Per Day使用 MySQL 限制每天发送的访问者
【发布时间】:2016-12-26 21:22:38
【问题描述】:

我正在建立一个使用 PHP 和 MySQLi 的网站。该网站向其他网站所有者出售流量。以下是我的表的结构,用于监控活跃的广告系列:

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'

我想做的是让人们可以选择在 1 到 90 天内传播点击量,这样他们就不会一下子被所有点击量淹没。所以,我打算把结构改成这样:

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'

period 列将代表广告系列应传播的天数。

但我认为添加两列可能会更好,一列将显示活动开始的时间 (start_time) 和活动应该完成的时间 (end_time)。

如果我有开始和结束列,MySQL 是否有任何方法可以数学计算活动是否落后于计划并选择它?

例如,如果有人订购了 10,000 个点击并希望它们在 5 天内交付。如果在第 4 天,他们只有 2000 次点击,显然它会“落后”,因为它应该有 8000-10000 次点击。

我知道有一些方法可以用 PHP 来计算。我只是想弄清楚是否有一种方法可以使用纯 MySQL 来选择所有落后于计划的行。

似乎应该有一种方法来查询这些行,因为 MySQL 可以执行数学运算,知道发送的总命中数、订单总金额以及开始/结束日期。

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

下面是@ICE 的另一个示例。这一行将落后于计划:

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

它落后了,因为自 23 日订购以来只发送了 5 次点击。现在是25号,28号结束。所以按照我上面的逻辑,应该算是“落后”了。

而这一行不会是:

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

在此示例中,日期相同,但已发送 1000 次点击中的 999 次。它不会落后于计划。

【问题讨论】:

  • 这与您的要求无关,但 status 类型对数据库规范化非常不利。如果只有 active 和 inactive 的值最好是 BOOLEAN。

标签: php mysql sql


【解决方案1】:

您可以比较剩余命中的百分比和campain的天数百分比,如果hits_sent的百分比高于campain持续时间百分比的百分比,只需用bool表达式禁用它。

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

SQLFIDDLE 中给出活动或非活动,并给你一个数字,表示活动是否按计划(接近 1)低于计划(接近 0)和高于 1,当它已经完成时

【讨论】:

  • 太棒了!正是我想要的!
【解决方案2】:

不需要列period。将end_timestart_time 创建为DATETIME。之后,您可以使用datediff 选择它们。它以天为单位返回两个日期之间的差异:

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

解释

到目前为止每天的点击次数

hits_send/datediff(now(),start_time)

剩余天数

datediff(end_time,now())

根据前几天发生的情况,我们可以猜测剩余几天可能会发生这么多点击

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

+20 是波动。我们猜也许有一天他们可以点击更多 20 次。

【讨论】:

  • 嗯,这几乎只是跟踪活动是否应该处于活动状态。我正在尝试做的是选择落后于计划的广告系列。
  • hits_sent 将始终小于或等于 hits_ordered。我只需要选择一个活动是否真的落后于其计划,就像我原始帖子中的示例一样。
  • @Andrew 编辑了我的答案。它将选择您在计划后调用的内容。我没有测试,只是在这里写的。我希望它对你有用。
猜你喜欢
  • 1970-01-01
  • 2019-02-15
  • 1970-01-01
  • 1970-01-01
  • 2011-07-19
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多