如何在MYSQL中获得第二个MAXIMUM DATE

sun*_*nny -6 php mysql max

我想从mysql 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 
    LEFT join
(
  select max(notification_date) notification_date,
    client_id
  from og_ratings
  WHERE notification_date NOT IN (select max(notification_date) FROM og_ratings )
   ) 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   
}
?>
Run Code Online (Sandbox Code Playgroud)

这是此代码的输出

在此输入图像描述

在输出图像中,您可以看到它从db获取所有记录.但我想只获取具有第二个最大日期的数据.

我怎么能这样做?

fth*_*lla 8

阅读您的查询并不好玩,但我认为问题在于:

LEFT JOIN (
  SELECT max(notification_date) notification_date, client_id
  FROM og_ratings
  WHERE notification_date NOT IN (
    SELECT max(notification_date)
    FROM og_ratings
)
Run Code Online (Sandbox Code Playgroud)

如果您想要GROUP BY client_id所需的每个客户端的最大日期:

SELECT client_id, max(notification_date) notification_date
FROM og_ratings
GROUP BY client_id
Run Code Online (Sandbox Code Playgroud)

如果你想要第二个最大值几乎没有选项,我使用的这个更容易理解,但它不一定是最高性能:

SELECT client_id, max(notification_date) notification_date
FROM og_ratings
WHERE
  (client_id, notification_date) NOT IN (
    SELECT client_id, max(notification_date)
    FROM og_ratings GROUP BY client_id
  )
GROUP BY client_id
Run Code Online (Sandbox Code Playgroud)

第三个问题,你正在使用LEFT JOIN,这意味着你将从og_ratings返回所有值,无论它们是否是第二个最大值.在此上下文中使用INNER JOIN:

SELECT
  r.client_id,
  c.id,
  t.id,
  ..etc...
FROM
  og_ratings r INNER JOIN (
    SELECT client_id, max(notification_date) notification_2nd_date
    FROM og_ratings
    WHERE
      (client_id, notification_date) NOT IN (
        SELECT client_id, max(notification_date)
        FROM og_ratings GROUP BY client_id
      )
    GROUP BY client_id
   ) r2
  ON r.notification_date = r2.notification_2nd_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
  )
Run Code Online (Sandbox Code Playgroud)