如何在sql中根据特定的日期范围进行过滤

t98cgbkg  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(288)

我试图从数据库表中从一个特定日期到另一个日期获取数据;但是当我过滤的时候,它不会产生任何结果。
在我的数据库中,日期的格式是2010-01-29 00:00:00.000,但我想使用y-m-d即2010-01-29进行过滤
以下是我的疑问:

$froms     = $_POST['from'];
 $tos       = $_POST['to'];

if($froms==""){
    $froms = "AND Loans.Disb_Date <= company.BRSES_DATE"; 
 }else{
    $froms =  "AND  Loans.Disb_Date <= '".$from."'";
 }

 if($to != ""){
    $tos =  "AND  Loans.Disb_Date = '".$to."'";
 }

以及sql查询

SELECT  ".$limitresult."
coa.loan_lmt,
company.BRSES_DATE,
Loans.Branch, 
Loans.GL_NO,
Loans.AC_No,
Loans.Cycle,
Loans.Disb_Date,
Loans.Loan_Amt+Loans.Tot_Int as Loan_amount_and_Interest_added,
Loans.Loan_Amt,
Loans.Tot_Int,
Loans.Exp_Date,
Loans.cust_type,
LN_Period AS 
LN_PERIODx, 
Inst_Type AS Inst_TYpeX,
Customer.Cust_No, Loans.fx_rate, 
Customer.Name, 
Ref_NO AS Ref_No8, 
Customer.Phone,
Loans.Officer,
Loans.LNP_Code,
Loans.bus_type,
bustype.descriptio,
inst_type, Loans.ln_period,
Loans.full_paid, Loans.Class_Age,
Loans.Gl_No+Loans.Ac_No+Loans.Branch+LTRIM(str(Loans.cycle)) AS LoanId,
member.ac_status, 
-SUM(CASE WHEN trx_type='LD' THEN LoanHist.principal+LoanHist.interest ELSE 0 END) -
SUM(CASE WHEN trx_type='LP' THEN LoanHist.principal+LoanHist.interest ELSE 0 END) AS outstanding_balance,
MAX(CASE WHEN trx_type='LD' THEN LoanHist.principal+LoanHist.interest ELSE 0 END) AS instalment_amount,  
-SUM(CASE WHEN trx_type='LD' THEN LoanHist.principal+LoanHist.interest ELSE 0 END) AS loan_amount,
MAX(CASE WHEN trx_type='LP' THEN trx_date ELSE NULL END) AS last_payment_date,
SUM(CASE WHEN trx_type='LP' THEN LoanHist.principal+LoanHist.interest ELSE 0 END) AS amount_paid,
-SUM(CASE WHEN trx_type='LD' AND trx_date < company.BRSES_DATE THEN LoanHist.principal+LoanHist.interest ELSE 0 END) -
SUM(CASE WHEN trx_type='LP' AND trx_date < company.BRSES_DATE THEN LoanHist.principal+LoanHist.interest ELSE 0 END) AS overdue
FROM Loans INNER JOIN Member 
ON Loans.Branch = Member.Branch AND Loans.GL_NO = Member.GL_NO AND Loans.AC_NO = MEMBER.AC_NO INNER JOIN Customer 
ON Member.Branch = Customer.Branch AND Member.CUST_NO = Customer.Cust_NO 
INNER JOIN Company ON Customer.Branch = Company.Branch 
INNER JOIN bustype ON loans.bus_type=bustype.code
INNER JOIN coa ON loans.gl_no = coa.gl_no
INNER JOIN Loanhist  ON LoanHist.Branch = Loans.Branch 
AND LoanHist.GL_NO = Loans.GL_NO 
AND LoanHist.AC_NO = Loans.AC_NO 
AND LoanHist.Cycle = Loans.Cycle 
WHERE Loans.Branch = company.Branch AND Loans.full_paid != 1 AND Company.Reg_Code = 0  ".$froms." ".$tos."
".$branchid." ".$accnos." ".$stas."
 GROUP BY LoanHist.Branch,LoanHist.GL_NO,LoanHist.AC_No,LoanHist.Cycle,Loans.branch,Loans.gl_no, Loans.ac_no, Loans.cycle,
Loans.disb_date, Loans.loan_amt, Loans.tot_int,Loans.exp_date,Loans.ln_period,Loans.inst_type,Customer.cust_no,Loans.fx_rate,
Customer.name, Customer.ref_no, Customer.phone,Loans.officer,Loans.LNP_Code,Loans.bus_type,bustype.descriptio,Loans.full_paid,
Loans.Class_Age,Member.ac_status,Loans.cust_type,company.BRSES_DATE, coa.loan_lmt

我做错什么了?

j0pj023g

j0pj023g1#

我不是用10英尺长的杆子来处理这个查询,但是一个简化的查询示例如下:

SELECT id FROM dbTable WHERE LEFT(dateStamp,10) = '2010-10-29';

如果你有 dateFrom 以及 dateTo 列,并且要查找对单个传递日期有效的结果,可以执行以下操作:

SELECT id FROM dbTable WHERE ('2010-01-29 00:00:00' BETWEEN dateFrom and dateTo)
ORDER BY dateTo DESC;

相关问题