从今天的日期检索大于2个月的mySQL数据

时间:2012-01-04 16:24:32

标签: php mysql

我想从mySQL数据库中提取数据,该数据库显示的成员的订阅日期已从当前日期过去2个月或更长时间。即我运行PHP报告的那一天。

我尝试使用下面的查询从我的数据库中提取数据,但它没有提取任何数据。

$result = mysql_query("SELECT userid, forename, surname, subscriptionexpiration FROM {$table} WHERE subscriptionexpiration BETWEEN DATE_SUB(now(), INTERVAL 2 MONTH) AND now() ORDER BY userid DESC"); 

日期作为ddmmyy存储在我的数据库中,因此我不确定这是否是问题或我的查询是否不正确。

完整脚本

<?php
$db_host = 'hostname';
$db_user = 'username';
$db_pwd = 'password';

$database = 'databasename';
$table = 'userdetails';
// use the same name as SQL table

if (!mysql_connect($db_host, $db_user, $db_pwd)) 
die("Can't connect to database"); 

if (!mysql_select_db($database)) 
die("Can't select database"); 


function sql_safe($s)  
{      if (get_magic_quotes_gpc())  
        $s = stripslashes($s);  

    return mysql_real_escape_string($s);  
}  

if ($_SERVER['REQUEST_METHOD'] == 'POST')  
{  
    if (isset($_POST["submit"]))  
    {  
        if (isset($_POST['del'])) 
            $userid = intval($_POST['del']); 

        if (mysql_query("DELETE FROM {$table} WHERE userid={$userid}")) 
            $msg = 'The member who you selected has now been deleted!';  
        else 
            $msg = 'Could not delete the selected member'; 
    } 
}  
?> 
<html><head> 
<title>Members With 2 Month Subscription Expiration</title>
<style type="text/css">
<!--
.style1 {
    color: #FFFFFF;
    font-size: 18px;
    font-family: Calibri;
}
.style2 {
    font-size: 18px;
    font-weight: bold;
    font-family: Calibri;
}
.style3 {font-family: Calibri}
.style6 {font-size: 16px}
.style7 {font-family: Calibri; font-size: 16px; }
-->
</style>
</head> 
<body>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p class="style7">
<?php 
if (isset($msg)) // this is special section for 
// outputing message 
{ 
?></p>
<p class="style7">
  <?=$msg?>
</p>
<?php 
} 
?>
<form action="<?=$PHP_SELF?>" method="POST" enctype="multipart/form-data"> 
<?php 
$result = mysql_query("SELECT userid, forename, surname, subscriptionexpiration FROM {$table} WHERE subscriptionexpiration > now() and datediff(month, now(), subscriptionexpiration) >= 2 ORDER BY userid DESC");    if (mysql_num_rows($result) == 0) // table is empty 
echo 'There are currently no members where their "Subscription Date" is greater than two months!'; 
else 
{ 
echo "<table>\n"; 
while(list($userid, $forename, $surname, $subscriptionexpiration) = mysql_fetch_row($result)) 
{ 
echo "<tr>\n" 
."<td><input type='radio' name='del' forename, surname value='{$userid}' /></td>\n" 
."<td><small>{$forename} {$surname}</small><td>\n" 
."<td><small>{$subscriptionexpiration}</small><td>\n" 
."</tr>\n"; 
} 
echo "<tr><td colspan=\"3\">"; 
echo '<input type="submit" value="Delete Selected Member" name="submit"/>'; 
echo "</td></tr>"; 
echo '</table>'; 
} 
?> 
<input type="hidden" name="action" id="action" /> 
</form> 
</body>
</html>

解决方案

("SELECT userid, forename, surname, subscriptionexpiration FROM {$table} WHERE subscriptionexpiration <NOW() - INTERVAL 2 MONTH ORDER BY userid DESC");

3 个答案:

答案 0 :(得分:1)

你的sql似乎是倒退的。您要求从现在起2个月到现在订阅的所有行,您将找不到。它应该是从现在到2个月之间。在dateA和dateB

之间使用时,始终从最早的日期开始

您可能需要在日期字段和日期A与日期B之间进行一些演员表以获得匹配的所有内容。

答案 1 :(得分:1)

在“where”子句中使用datediff。这看起来像

$result = mysql_query(
      "SELECT userid, forename, surname, subscriptionexpiration 
         FROM {$table}  
         WHERE subscriptionexpiration > now() and  
             datediff(month, now(), subscriptionexpiration) <= -2 ORDER BY userid DESC");

答案 2 :(得分:1)

尝试此查询 -

SELECT
  userid, forename, surname, subscriptionexpiration
FROM
  table_name
WHERE
  STR_TO_DATE(subscriptionexpiration, '%d%m%y') BETWEEN NOW() - INTERVAL 2 MONTH AND NOW()
ORDER BY
  userid DESC