我正在努力分别生成每1,3,6,12和24个月生成报告的要求,用户通过选择下拉菜单选择他的选择。首先,我写了一个查询来生成1个月的报告,这很好。但我不确定如何根据用户选择动态选择。
我的查询如下:
switch($months){
case "1":
$stmt = $pdo->prepare("SELECT count(*) as totalrows FROM today_analysis WHERE order_status=:order_status AND MONTH(track_date)=MONTH(CURDATE())");
break;
case "3":
$stmt = $pdo->prepare("SELECT count(*) as totalrows FROM today_analysis WHERE order_status=:order_status AND CEIL(MONTH(track_date) / 3)");
break;
case "6":
$stmt = $pdo->prepare("SELECT count(*) as totalrows FROM today_analysis WHERE order_status=:order_status AND CEIL(MONTH(track_date) / 6)");
break;
case "12":
$stmt = $pdo->prepare("SELECT count(*) as totalrows FROM today_analysis WHERE order_status=:order_status AND CEIL(MONTH(track_date) / 12)");
break;
case "24":
$stmt = $pdo->prepare("SELECT count(*) as totalrows FROM today_analysis WHERE order_status=:order_status AND CEIL(MONTH(track_date) / 24)");
break;
}
我的选择下拉列表是:
<select name="month_analysis" id="month_analysis" class="month_analysis">
<option value="1" <?php echo (isset($_GET['monthly']) && $_GET['monthly']=='1')?'selected':''?>>Current Month</option>
<option value="3" <?php echo (isset($_GET['monthly']) && $_GET['monthly']=='3')?'selected':''?> >3 Months</option>
<option value="6" <?php echo (isset($_GET['monthly']) && $_GET['monthly']=='6')?'selected':''?> >6 Months</option>
<option value="12" <?php echo (isset($_GET['monthly']) && $_GET['monthly']=='12')?'selected':''?> >12 Months</option>
<option value="24" <?php echo (isset($_GET['monthly']) && $_GET['monthly']=='24')?'selected':''?> >24 Months</option>
</select>
目前,该查询似乎正在运行,但我不确定它是否正常工作。请提出建议/帮助吗?感谢。
答案 0 :(得分:1)
您可以使用mysql&#39; date_sub() function来计算哪些记录属于所需的日期范围:
SELECT count(*) as totalrows FROM today_analysis WHERE order_status=:order_status AND track_date>=date_sub(curdate(),interval 1 month)
根据下拉菜单中的值,您可以在值1
的位置动态分配date_sub()函数中的月数:
SELECT count(*) as totalrows FROM today_analysis WHERE order_status=:order_status AND track_date>=date_sub(curdate(),interval :monthes month)