我是php新手。我有一个mysql数据库。我想从db获取最新记录和一个选定列的先前记录。我在我的查询中使用INNER加入
SELECT r.client_id,c.id,t.id,a.id,o.id,c.name as cname,t.title as ttitle,a.title as atitle,o.title as otitle, l.title as ltitle,s.title as stitle
FROM og_ratings r
INNER JOIN og_companies c
ON r.client_id = c.id
INNER JOIN og_rating_types t
ON r.rating_type_id = t.id
INNER JOIN og_actions a
ON r.pacra_action = a.id
INNER JOIN og_outlooks o
ON r.pacra_outlook = o.id
INNER JOIN og_lterms l
ON r.pacra_lterm = l.id
INNER JOIN og_sterms s
ON r.pacra_sterm = s.id
WHERE c.id= 248
ORDER BY r.id DESC
LIMIT 1
打印数据我的php代码是
while ($row = $result->fetch_assoc()){
echo '<h1>' .$row["client_id"]. '</h1>' ;
echo '<h1>' .$row["cname"]. '</h1>' ;
echo '<h1>' .$row["ttitle"]. '</h1>' ;
echo '<h1>' .$row["atitle"]. '</h1>' ;
echo '<h1>' .$row["otitle"]. '</h1>' ;
echo '<h1>' .$row["ltitle"]. '</h1>' ;
echo '<h1>' .$row["stitle"]. '</h1>' ;
我希望在ltitle
和stitle
中显示最新和之前的一条记录
答案 0 :(得分:0)
试试这个:
$last_row = array_pop($row);
echo '<h1>' .$last_row["client_id"]. '</h1>' ;
echo '<h1>' .$last_row["cname"]. '</h1>' ;
echo '<h1>' .$last_row["ttitle"]. '</h1>' ;
echo '<h1>' .$last_row["atitle"]. '</h1>' ;
echo '<h1>' .$last_row["otitle"]. '</h1>' ;
echo '<h1>' .$last_row["ltitle"]. '</h1>' ;
echo '<h1>' .$last_row["stitle"]. '</h1>' ;
$before_last = array_pop($row);
echo '<h1>' .$before_last["client_id"]. '</h1>' ;
echo '<h1>' .$before_last["cname"]. '</h1>' ;
echo '<h1>' .$before_last["ttitle"]. '</h1>' ;
echo '<h1>' .$before_last["atitle"]. '</h1>' ;
echo '<h1>' .$before_last["otitle"]. '</h1>' ;
echo '<h1>' .$before_last["ltitle"]. '</h1>' ;
echo '<h1>' .$before_last["stitle"]. '</h1>' ;