我是 php 新手。我有一个 mysql 数据库。我想从数据库获取最新记录和所选列的前一条记录。我在查询中使用 INNER Join
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
以及最新和上一条记录
最佳答案
试试这个:
$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>' ;
关于php - 如何打印最新一行和所选列的前一个数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31559014/