根据日期范围从月表MySQL中获取数据(2016年1月1日至2016年4月1日之间的前数据)

时间:2017-03-22 07:01:17

标签: php mysql

我有月度数据库表,用于存储我的网站的每日用户注册数。在我的UI设计中,我有两个日期之间的日期范围,我必须在提供的日期范围之间获得注册用户的总和。(但是会有4或5日期范围之间的许多月(基于日期范围))我将如何获取数据?我需要优化的解决方案,以便从多个mysql表中获取数据总和。

CREATE TABLE daily_analytics_01_2017 ( 
id int(11) NOT NULL AUTO_INCREMENT, 
country varchar(255) DEFAULT NULL, 
device varchar(255) DEFAULT NULL, 
browser varchar(255) DEFAULT NULL, 
gender varchar(255) DEFAULT NULL, u
ser_loginCount int(11) NOT NULL, 
user_signup_count int(11) NOT NULL, 
tracking_date date NOT NULL, 
PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1;

上表中的每个月名称如

daily_analytics_MONTH_YEAR

以及我网站上登录和其他操作的所有数据,我必须每月跟踪。

4 个答案:

答案 0 :(得分:1)

使用以下函数构建查询:

function getMonthWiseQuery($fromdate,$todate)
{

    $fromexplode=explode("-",$fromdate);
    $startyear=$fromexplode[0];
    $startmonth=$fromexplode[1];
    $startdate=$fromexplode[2];


    $toexplode=explode("-",$todate);
    $endyear=$toexplode[0];
    $endmonth=$toexplode[1];
    $enddate=$toexplode[2];

    $queryBuild=array();
    $startmonthnew = $startmonth;
    $count=0;
    for ($i = $startyear; $i <= $endyear; $i++) {
        for ($j = $startmonthnew; $j < 13; $j++) {

                $count++;
                if ($fromdate) {

                    if (strlen($j) == 1) {
                        $j = "0" . $j;
                    }
                    if($count!=1)
                    {
                        $query=" UNION ALL";
                        $query.= " select sum(user_with_referral) as Total_UserBy_referral,sum(total_unverified_users) as TotalUnverifiedUsers,sum(user_signup_count) as TotalRegisteredUsers,sum(user_signUpactivationSuccess_count) as TotalActivatedUsers from daily_analytics_{$j}_{$i} where tracking_date BETWEEN \"{$fromdate}\" and \"{$todate}\" ";
                    }else{
                        $query = "select sum(user_with_referral) as Total_UserBy_referral,sum(total_unverified_users) as TotalUnverifiedUsers,sum(user_signup_count) as TotalRegisteredUsers,sum(user_signUpactivationSuccess_count) as TotalActivatedUsers from daily_analytics_{$j}_{$i} where tracking_date BETWEEN \"{$fromdate}\" and \"{$todate}\" ";
                    }

                    array_push($queryBuild,$query);

                }

            if ($j == 12) {
                $startmonthnew = 1;
                break;
            }

            if ($endyear == $i) {
                if ($j == $endmonth) {
                    break;
                }
            }

        }
    }

return implode(" ",$queryBuild);
}

函数将返回与日期范围中的所有表的联合查询 -

    select sum(user_with_referral) as Total_UserBy_referral,sum(total_unverified_users) as TotalUnverifiedUsers,sum(user_signup_count) as TotalRegisteredUsers,sum(user_signUpactivationSuccess_count) as TotalActivatedUsers from daily_analytics_01_2017 where tracking_date BETWEEN "2017-01-01" and "2017-03-21"  
UNION ALL 
select sum(user_with_referral) as Total_UserBy_referral,sum(total_unverified_users) as TotalUnverifiedUsers,sum(user_signup_count) as TotalRegisteredUsers,sum(user_signUpactivationSuccess_count) as TotalActivatedUsers from daily_analytics_02_2017 where tracking_date BETWEEN "2017-01-01" and "2017-03-21"  
UNION ALL 
select sum(user_with_referral) as Total_UserBy_referral,sum(total_unverified_users) as TotalUnverifiedUsers,sum(user_signup_count) as TotalRegisteredUsers,sum(user_signUpactivationSuccess_count) as TotalActivatedUsers from daily_analytics_03_2017 where tracking_date BETWEEN "2017-01-01" and "2017-03-21" 

然后,根据此查询,我们可以从表格的UNION中获取总结果的总和 -

   select sum(Total_UserBy_referral) as Total_UserBy_referral , sum(TotalUnverifiedUsers) as TotalUnverifiedUsers,sum(TotalRegisteredUsers) as TotalRegisteredUsers, sum(TotalActivatedUsers) as TotalActivatedUsers from (
select sum(user_with_referral) as Total_UserBy_referral,sum(total_unverified_users) as TotalUnverifiedUsers,sum(user_signup_count) as TotalRegisteredUsers,sum(user_signUpactivationSuccess_count) as TotalActivatedUsers from daily_analytics_01_2017 where tracking_date BETWEEN "2017-01-01" and "2017-03-21"  
UNION ALL 
select sum(user_with_referral) as Total_UserBy_referral,sum(total_unverified_users) as TotalUnverifiedUsers,sum(user_signup_count) as TotalRegisteredUsers,sum(user_signUpactivationSuccess_count) as TotalActivatedUsers from daily_analytics_02_2017 where tracking_date BETWEEN "2017-01-01" and "2017-03-21"  
UNION ALL 
select sum(user_with_referral) as Total_UserBy_referral,sum(total_unverified_users) as TotalUnverifiedUsers,sum(user_signup_count) as TotalRegisteredUsers,sum(user_signUpactivationSuccess_count) as TotalActivatedUsers from daily_analytics_03_2017 where tracking_date BETWEEN "2017-01-01" and "2017-03-21" 
) as t

答案 1 :(得分:0)

如果将所有注册存储在一个表中,会不会更好?查询和维护更容易。

// the selected range will have to be formatted
$from = '2017-01-01 00:00:00';
$to = '2017-03-15 23:59:59';

// you will have to run this query just once on one particular table 
$query = "SELECT count(*) as number_of_signups FROM tablename WHERE created_on BETWEEN $from AND $to";

如果您希望每月在不同的表中存储注册,那么您可以使用时间戳列上的数据范围查询每个表。

// initialization
$count = 0;

// the selected range will have to be formatted
$from = '2017-01-01 00:00:00';
$to = '2017-03-15 23:59:59';

$con=mysqli_connect("yourhost","dbuser","password","your_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// run this query on all the signup tables 
$query = "SELECT count(*) as number_of_signups FROM tablename WHERE created_on BETWEEN $from AND $to";

$result=mysqli_query($con,$query);

// Associative array
$row=mysqli_fetch_assoc($result);
$count += $row['number_of_signups'];

// Free result set
mysqli_free_result($result);

mysqli_close($con);

正如您所看到的,这部分变得非常冗长和复杂:

// run this query on all the signup tables 
$query = "SELECT count(*) as number_of_signups FROM tablename WHERE created_on BETWEEN $from AND $to";

$result=mysqli_query($con,$query);

// Associative array
$row=mysqli_fetch_assoc($result);
$count += $row['number_of_signups'];

答案 2 :(得分:0)

$sql = "SELECT  id, country,device,     
            LOWER(MONTHNAME(tracking_date)) AS month,
            YEAR(tracking_date) AS `year`,
            SUM(user_signup_count) AS `monthly_sum`
        FROM daily_analytics_01_2017
        WHERE tracking_date BETWEEN '$first' AND '$last'
        GROUP BY month";

说明: 明智地创建表,创建单个表并使用它是不是一个好主意。以上查询就足够了。

$first - start date
$last - end date

答案 3 :(得分:0)

检查此代码,它将显示所有之前的12个月登录用户注册信息。您需要将此代码更改为表字段和表名。

select date_year y, date_month m, count(*) cnt
            from
            (select id, year(access_time) date_year, month(access_time) date_month, access_time, uid
            from users_login_detail_history
            where date(access_time) >= date_format(concat (year(date_add(date(now()), interval -1 year)), '-' , month(date_add(date(now()), interval -1 year)) , '-1'), '%Y-%m-%d')) login_data
            group by date_year, date_month
            order by date_year, date_month;

从这个sql这是输出结果 enter image description here