【问题标题】:How to get second latest row from mysql? [duplicate]如何从mysql获取第二个最新行? [复制]
【发布时间】:2015-09-11 05:30:30
【问题描述】:

我是 php 新手。我想从我的数据库中打印第二个最新的行。

使用以下代码,我从 db 中打印出最新的第一行

<?php
include ("connection.php");
$q_opinion="SELECT r.client_id,c.id,t.id,a.id,o.id,c.name as opinion, r.notification_date, t.title as ttitle,a.title as atitle,o.title as otitle, l.title as ltitle, s.title as stitle, pr.opinion_id, pc.id, pr.client_id as pr_client, pc.address, pc.liaison_one, city.id, pc.head_office_id, city.city, pc.title as cname
FROM og_ratings r 
    inner join
(
  select max(notification_date) notification_date,
    client_id
  from og_ratings
  group by client_id
  ORDER BY notification_date DESC
) r2
  on r.notification_date = r2.notification_date
  and r.client_id = r2.client_id
LEFT JOIN og_companies c
ON r.client_id = c.id
LEFT JOIN og_rating_types t
ON r.rating_type_id = t.id
LEFT JOIN og_actions a
ON r.pacra_action = a.id
LEFT JOIN og_outlooks o
ON r.pacra_outlook = o.id
LEFT JOIN og_lterms l
ON r.pacra_lterm = l.id
LEFT JOIN og_sterms s
ON r.pacra_sterm = s.id
LEFT JOIN pacra_client_opinion_relations pr
ON pr.opinion_id = c.id
LEFT JOIN pacra_clients pc
ON pc.id = pr.client_id
LEFT JOIN city
ON city.id = pc.head_office_id
WHERE r.client_id  IN (SELECT opinion_id FROM pacra_client_opinion_relations WHERE client_id = 50)";
$result = mysql_query($q_opinion) or die;
$rating = array();
while($row = mysql_fetch_assoc($result))
{
  $rating[] = $row['client_id'];
  $action[] = $row['atitle'];
  $opinion[] = $row['opinion'];
  $date[] = $row['notification_date'];
  $lrating[] = $row['ltitle'];
  $srating[] = $row['stitle'];
}
for ($i=0; $i<count($rating); $i++) {
    if ($rating[$i] == "")continue;
     ?>
    <table border="1">
    <tr>
          <td><?= $rating[$i] ?> </td>
           <td><?= $date[$i] ?> </td>
          <td><?= $opinion[$i] ?> </td>
         <td><?= $action[$i] ?> </td>
          <td><?= $lrating[$i] ?> </td>
           <td><?= $srating[$i] ?> </td>
    </tr>
    </table>
<?php   
}
?>

我的代码中的以下几行

WHERE r.client_id  IN (SELECT opinion_id FROM pacra_client_opinion_relations WHERE client_id = 50)";

有多个结果

我的代码结果是

这是我数据库的第一条最新记录

现在我想直接获取第二条最新记录。我不知道我该怎么做。谁能帮帮我吗?

【问题讨论】:

  • 你可以使用 order by id desc 和 limit 2,1
  • 我在哪里可以在查询中使用 order by?

标签: php mysql sql


【解决方案1】:

在你的 SQL 查询末尾添加 order by client_id desc limit 1,1

【讨论】:

  • 它只打印一条记录。我在我的问题中提到我的子查询包含多条记录
【解决方案2】:

请试试这个

$q_opinion="SELECT r.client_id,c.id,t.id,a.id,o.id,c.name as opinion, r.notification_date, t.title as ttitle,a.title as atitle,o.title as otitle, l.title as ltitle, s.title as stitle, pr.opinion_id, pc.id, pr.client_id as pr_client, pc.address, pc.liaison_one, city.id, pc.head_office_id, city.city, pc.title as cname
FROM og_ratings r 
inner join
(
  select max(notification_date) notification_date,
  client_id
  from og_ratings
  group by client_id
  ORDER BY notification_date DESC
) r2
  on r.notification_date = r2.notification_date
  and r.client_id = r2.client_id
LEFT JOIN og_companies c
ON r.client_id = c.id
 LEFT JOIN og_rating_types t
ON r.rating_type_id = t.id
LEFT JOIN og_actions a
ON r.pacra_action = a.id
LEFT JOIN og_outlooks o
ON r.pacra_outlook = o.id
LEFT JOIN og_lterms l
ON r.pacra_lterm = l.id
LEFT JOIN og_sterms s
ON r.pacra_sterm = s.id
LEFT JOIN pacra_client_opinion_relations pr
ON pr.opinion_id = c.id
LEFT JOIN pacra_clients pc
ON pc.id = pr.client_id
LEFT JOIN city
ON city.id = pc.head_office_id
WHERE r.client_id  IN (SELECT opinion_id FROM pacra_client_opinion_relations WHERE client_id = 50) ORDER BY c.id DESC LIMIT 1, 1

【讨论】:

  • 它只打印单条记录,但正如我在问题中提到的,我的子查询包含多条记录,我想全部打印
  • 所有记录的第二行
  • 您需要偶数记录吗?
  • 不,我只需要数据库中的两行。我得到了第一行,但现在我想得到第二行
【解决方案3】:

例子

SELECT * FROM  `users` ORDER BY  `id` DESC LIMIT 1 , 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多