2013-05-12 39 views
0

我需要這個查詢的幫助,這個工作正常,事情是它返回我的一切從ingresos。我只需要檢索上個月和當年的記錄。從上個月和當前年份過濾記錄

我已嘗試INTERVAL 1 MONTH,但它會根據今天的日期返回前一個月。 我需要上個月的記錄,不需要從第一個到最後一個日期。

這一個是當E頁面加載第一:

SELECT * from ingresos 
where fechaReporteING < DATE_SUB(CURDATE(),INTERVAL DAYOFMONTH(CURDATE()) DAY) 

和當用戶選擇特定月份這個人是:

SELECT * from ingresos 
where fechaReporteING < DATE(CONCAT(YEAR(CURDATE()),"-",'.$_POST['mes'].',"-","0")) 

回答

0

這對我的作品。它使用PHP根據用戶選擇的月份計算日期。

<?php 
//Get your users month. 
$month = isset($_GET['mes'])? $_GET['mes'] : "04"; 
//Starting date string `current year`-`user month`-`first day` 
$startDateText = date('Y') . "-" . $month . "-01"; 

//Add one month to the starting date. 
$startDate = date_create($startDateText); 
$endDate = date_add($startDate, date_interval_create_from_date_string('1 month')); 

//String representation of the date 
$endDateText = date_format($endDate, 'Y-m-d'); 

echo "<br><br>Search for records on or after $startDateText but before $endDateText.<br><br>"; 

$query = "SELECT * FROM ingresos 
    WHERE fechaReportING >= '$startDateText' && fechaReportING < '$endDateText' "; 

$allData = mysqli_query($db_conx, $query) or printf(mysqli_error($db_conx)); 
$totalDataRows = mysqli_num_rows($allData); 
.... 
.... 
?> 
相關問題