我有一张费用表。每当学生付费时,它将存储在此表中。
feeid student_id paid_date received_amount balance_amount next_due_date
3 1 2015-11-07 1000 15000 2015-11-01
5 2 2015-11-07 2000 14000 2015-11-02
9 3 2015-11-07 30000 15000 2015-11-07
11 1 2015-11-07 1000 14000 2015-07-11
12 4 2015-11-07 1000 40000 2015-11-07
13 4 2015-11-07 1000 39000 2015-12-01
14 3 2015-11-07 1000 14000 2015-11-30
15 5 2015-11-09 1000 25000 2015-11-09
16 1 2015-11-09 5000 9000 2015-11-30
17 2 2015-11-09 1000 13000 2015-11-15
18 6 2015-11-10 1000 20000 2015-11-10
19 1 2015-11-11 1000 8000 2015-11-11
20 4 2015-11-11 1000 38000 2015-11-11
21 7 2015-11-11 1000 24000 2015-11-11
22 8 2015-11-11 1000 19500 2015-11-11
23 1 2015-11-14 1000 7000 2015-11-16
所以一个单一的条目不止一个。我需要每个学生的费用详情(只有最后付费的详细信息)
答案 0 :(得分:0)
这是一种方式:
SELECT *
FROM table t1
WHERE paid_date=(SELECT MAX(t2.paid_date)
FROM table t2
WHERE t1.student_id = t2.student_id);
答案 1 :(得分:-1)
首先获得所有独特的学生ID。
之后循环
SELECT * FROM table_name WHERE student_id = studentIdFromLoop ORDER BY feeid DESC LIMIT 1
这将为您提供每位学生的最后费用记录。