简体   繁体   English

mysql查询以获取特定日期之间的用户详细信息

[英]mysql query to fetch details of users between specific dates

$query="SELECT DISTINCT ind_id,indentdate,submitted_by 
        FROM raiseindent 
        where submitted_by='$nm' 
          AND indentdate BETWEEN '$fromDate' AND '$toDate' 
          OR indentdate BETWEEN '$fromDate' AND '$toDate' 
          OR submitted_by='$nm'";

here $nm is username 这里$nm是用户名

I am not getting any errors, this query fetching records if I use only $nm (User Name) or between two dates 我没有收到任何错误,如果我仅使用$nm (用户名)或两个日期之间,此查询将获取记录

If I use to serach specific user name and two dates it is fetching all users records between selected dates 如果我用来搜索特定的用户名和两个日期,它将在选定的日期之间获取所有用户的记录

I need specific only the results from entered user name in username field to serach 我只需要从用户名字段中输入的用户名到搜索结果的特定结果

Try this 尝试这个

$query="SELECT DISTINCT ind_id,indentdate,submitted_by 
        FROM raiseindent 
        where submitted_by='$nm' 
          AND indentdate BETWEEN '$fromDate' AND '$toDate'";

I dont think it need to be any more complicated than this. 我认为这不需要比这更复杂。

Or if it is possible for $nm to be empty or the dates to be empty then do those tests in PHP and dynamically build the query accordingly rather than trying to get the SQL to do it all for you 或者,如果$nm可能为空或日期为空,则在PHP中进行这些测试并相应地动态构建查询,而不是尝试让SQL为您完成所有操作

$sql = 'SELECT DISTINCT ind_id,indentdate,submitted_by 
    FROM raiseindent 
    WHERE ';


if ( !empty($nm) && $nm != 'ALL' ) {
    $sql .= "submitted_by='$nm' AND ";
}
if ( !empty($fromDate) && !empty($toDate) ) {
    $sql .= " indentdate BETWEEN '$fromDate' AND '$toDate' ";
}
$sql = rtrim($sql, 'AND ');

Though it's already answered; 虽然已经回答了; for a note try using >= and <= to have the exact start and end date inclusive, instead BETWEEN operator like 要获得注释,请尝试使用>=<=来包含确切的开始和结束日期,而不是像BETWEEN运算符

SELECT DISTINCT ind_id,indentdate,submitted_by 
        FROM raiseindent 
        where submitted_by='$nm' 
          AND (indentdate >= '$fromDate' AND indentdate <= '$toDate');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM