我是php新手。我有个问题。我想从Mysql数据库中获取数据并在我的PHP页面上打印它。我想打印最新记录和所选列的一些先前数据。例如,我有两个表
table1( id, student_name )
table2: ( id, student_id, English, Math, Physics......., Total Marks, Obtain Marks and grade)
问题:我希望我可以获取全新的记录,但我希望我还可以获取该学生的上一年级。
有可能吗?我怎么做?
答案 0 :(得分:1)
使用
SELECT * FROM (
SELECT * FROM table2 WHERE (student_id=2) ORDER BY id DESC LIMIT 2
) sub
ORDER BY id ASC
您应该能够选择最后两行。
编辑语法格式:
$result = mysql_query($sql);
$rows = mysql_fetch_assoc($result);
echo $rows[0]["grade"]; // new grade
echo $rows[1]["grade"]; // previous grade
答案 1 :(得分:0)
您可以尝试以下操作:
$query = SELECT t1.*,t2.* FROM table1 as t1
LEFT JOIN table2 as t2 ON t1.id = t2.student_id
where t1.id = '1' ORDER BY t2.student_id desc LIMIT 2
$result = mysql_query($query);
$fetch_record = mysql_fetch_assoc($result);
$grade = $fetch_record[0]['pass-your-column-name'];