0

请帮助我,下面是我的 sql 查询。当我执行它时,结果什么都没有。

SELECT tblclientinfo.acctnum, 
       tblclientinfo.fname, 
       tblclientinfo.fname, 
       tblreservation.unitnum, 
       tblbillingsched.billnum, 
       tblbillingsched.duedate, 
       tblbillingsched.monthlyamort, 
       tblbillingsched.totalbalance 
FROM   tblclientinfo 
       JOIN tblreservation 
         ON tblclientinfo.acctnum = tblreservation.reservationnum 
       JOIN tblbillingsched 
         ON tblreservation.reservationnum = tblbillingsched.accountnum 
WHERE  tblbillingsched.accountnum = 'C0000000021' 
       AND tblbillingsched.duedate BETWEEN '1/1/2014' AND '1/30/2014'

这是我的表:

tblClientInfo, pk = acctNum
tblReservation, pk = ReservationNum, fk = accountNum
tblBillingSched, pk = ID,  fk = accountNum
4

4 回答 4

1

我想你正在寻找...

SELECT tblclientinfo.acctnum, 
       tblclientinfo.fname, 
       tblclientinfo.fname, 
       tblreservation.unitnum, 
       tblbillingsched.billnum, 
       tblbillingsched.duedate, 
       tblbillingsched.monthlyamort, 
       tblbillingsched.totalbalance 
FROM   tblclientinfo 
       JOIN tblreservation 
         ON tblclientinfo.acctnum = tblreservation.accountnum 
       JOIN tblbillingsched 
         ON tblclientinfo.acctnum = tblbillingsched.accountnum 
WHERE  tblbillingsched.accountnum = 'C0000000021' 
       AND tblbillingsched.duedate BETWEEN '1/1/2014' AND '1/30/2014'

查看演示

于 2013-09-26T13:36:21.363 回答
0

您的第一次加入是错误的。在表 tblReservation 中,外键是 accountNum,您使用字段 reservationnum 进行连接。代码应该是这样的:

SELECT tblclientinfo.acctnum, 
    `tblclientinfo.fname, 
     tblclientinfo.fname, 
     tblreservation.unitnum, 
     tblbillingsched.billnum, 
     tblbillingsched.duedate, 
     tblbillingsched.monthlyamort, 
     tblbillingsched.totalbalance 
FROM tblclientinfo 
     JOIN tblreservation 
     ON tblclientinfo.acctnum = tblreservation.accountNum 
     JOIN tblbillingsched 
     ON tblreservation.reservationnum = tblbillingsched.accountnum 
WHERE  tblbillingsched.accountnum = 'C0000000021' 
       AND tblbillingsched.duedate BETWEEN '1/1/2014' AND '1/30/2014'
于 2013-09-26T13:47:49.767 回答
0

DueDate 是日期时间字段吗?试试以下方法,看看它是否有效:

CAST(tblbillingsched.duedate AS DATE)在“2014 年 1 月 1 日”和“2014 年 1 月 30 日”之间

查看这篇文章以获得更详细的讨论:

如何按日期查询 Datetime 对象?

于 2013-09-26T14:01:36.237 回答
0

(题外话,可能不是答案,但我认为值得注意)

更安全的形式:

tblbillingsched.duedate BETWEEN '1/1/2014' AND '1/30/2014'

这是:

tblbillingsched.duedate >= '20140101' AND 
tblbillingsched.duedate <'20140130'

说出这样的日期'20140130'是明确无误的。

于 2013-09-26T14:42:57.757 回答