簡體   English   中英

如何在 MYSQL 中獲得第二個 MAXIMUM DATE

[英]How to get second MAXIMUM DATE in MYSQL

我想從 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   
}
?>

這是此代碼的輸出

在此處輸入圖片說明

在輸出圖像中,您可以看到它從數據庫中獲取所有記錄。 但我只想獲取具有第二個最大日期的數據。

我該怎么做?

閱讀您的查詢並不有趣,但我認為問題出在這里:

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
)

如果您想要每個客戶的最大日期,您需要按 client_id 分組:

SELECT client_id, max(notification_date) notification_date
FROM og_ratings
GROUP BY client_id

如果您想要第二個最大值,則選項很少,我正在使用這個更容易理解但不一定性能最高的選項:

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

第三個問題,您使用的是 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
  )

請注意,模式設置、 QueryAQueryB都只是用於可視化。

QueryC是您嘗試使用數據的方法。

這不會像@Musa 那樣做一個簡單的order by 和limit原因很簡單:你可以有很多行的第二大日期,而不是一個。 這就是為什么使用@grp 和@prevdate 的變量來定位第二組的原因。

架構設置

-- drop table specimenA;
create table specimenA
(   mypk int auto_increment primary key,
    id int not null,    -- note, not autoinc or pk
    theDate date not null,
    title varchar(255) not null,
    otherThing varchar(20) not null
    -- etc
    -- not other indexes whatsoever
);
-- truncate table specimenA
insert specimenA (id,theDate,title,otherThing) values
(170,'2007-09-19','whatever','whatever'),
(170,'2008-09-12','whatever','whatever'),
(170,'2010-01-15','whatever','whatever'),
(170,'2011-02-03','whatever','whatever'),
(170,'2012-06-26','whatever','whatever'),
(170,'2013-03-05','whatever','whatever'),
(170,'2014-06-25','whatever','whatever'),
(170,'2015-06-09','whatever','whatever'),

(917,'2009-10-14','whatever','whatever'),
(917,'2008-12-31','whatever','whatever'),

(109,'2010-04-26','whatever','whatever'),
(109,'2011-03-02','whatever','whatever'),
(109,'2012-06-25','whatever','whatever'),
(109,'2013-01-04','whatever','whatever'),
(109,'2014-03-28','whatever','whatever'),
(109,'2015-03-18','whatever','whatever'),

(1057,'2014-03-28','whatever','whatever'),
(1057,'2014-11-21','whatever','whatever'),
(1057,'2015-08-13','whatever','whatever');

查詢A

set @rn:=0,@grp:=0,@prevdate:=''; 
select id,theDate,title,otherThing,
@rn:=@rn+1 as rownum, 
@grp:=if(@prevdate=theDate,@grp,@grp+1) as descGrp, 
@prevdate:=theDate as unused 
from specimenA 
order by theDate DESC -- **** Note this
-- DESC means greatest first, as is most-recent first for dates
+------+------------+----------+------------+--------+---------+------------+
| id   | theDate    | title    | otherThing | rownum | descGrp | unused     |
+------+------------+----------+------------+--------+---------+------------+
| 1057 | 2015-08-13 | whatever | whatever   |      1 |       1 | 2015-08-13 |
|  170 | 2015-06-09 | whatever | whatever   |      2 |       2 | 2015-06-09 |
|  109 | 2015-03-18 | whatever | whatever   |      3 |       3 | 2015-03-18 |
| 1057 | 2014-11-21 | whatever | whatever   |      4 |       4 | 2014-11-21 |
|  170 | 2014-06-25 | whatever | whatever   |      5 |       5 | 2014-06-25 |
| 1057 | 2014-03-28 | whatever | whatever   |      6 |       6 | 2014-03-28 |
|  109 | 2014-03-28 | whatever | whatever   |      7 |       6 | 2014-03-28 |
|  170 | 2013-03-05 | whatever | whatever   |      8 |       7 | 2013-03-05 |
|  109 | 2013-01-04 | whatever | whatever   |      9 |       8 | 2013-01-04 |
|  170 | 2012-06-26 | whatever | whatever   |     10 |       9 | 2012-06-26 |
|  109 | 2012-06-25 | whatever | whatever   |     11 |      10 | 2012-06-25 |
|  109 | 2011-03-02 | whatever | whatever   |     12 |      11 | 2011-03-02 |
|  170 | 2011-02-03 | whatever | whatever   |     13 |      12 | 2011-02-03 |
|  109 | 2010-04-26 | whatever | whatever   |     14 |      13 | 2010-04-26 |
|  170 | 2010-01-15 | whatever | whatever   |     15 |      14 | 2010-01-15 |
|  917 | 2009-10-14 | whatever | whatever   |     16 |      15 | 2009-10-14 |
|  917 | 2008-12-31 | whatever | whatever   |     17 |      16 | 2008-12-31 |
|  170 | 2008-09-12 | whatever | whatever   |     18 |      17 | 2008-09-12 |
|  170 | 2007-09-19 | whatever | whatever   |     19 |      18 | 2007-09-19 |
+------+------------+----------+------------+--------+---------+------------+

並且,采用上面的 select 語句,並將其作為派生表作為別名inR ,嵌套:

查詢B

set @rn:=0,@grp:=0,@prevdate:=''; 
select id,theDate,title,otherthing 
from 
(   select id,theDate,title,otherThing,
    @rn:=@rn+1 as rownum, 
    @grp:=if(@prevdate=theDate,@grp,@grp+1) as descGrp, 
    @prevdate:=theDate as unused 
    from specimenA 
    order by theDate DESC  -- **** Note this
) inR 
where descGrp=2;
+-----+------------+----------+------------+
| id  | theDate    | title    | otherthing |
+-----+------------+----------+------------+
| 170 | 2015-06-09 | whatever | whatever   |
+-----+------------+----------+------------+

還有你第二個最棒的約會。 意思是第二個最近的日期。


所以,以你原來的 select 語句,同樣的概念。 顯示上述內容的動機很簡單: InR只是一個Derived Table ,不會多於或少於將成為派生表的 select 語句。

查詢C

set @rn:=0,@grp:=0,@prevdate:=''; 

select client_id, cid, tid, aid, oid, opinion, notification_date,
ttitle, atitle, otitle, ltitle, stitle,
opinion_id, pcid, pr_client, address, liaison_one, 
cityid, head_office_id, city, cname
from
(   SELECT r.client_id as client_id,c.id as cid,t.id as tid,a.id as aid,o.id as oid,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 as pcid, pr.client_id as pr_client, pc.address, pc.liaison_one, 
    city.id as cityid, pc.head_office_id, city.city, pc.title as cname,
    @rn:=@rn+1 as rownum, 
    @grp:=if(@prevdate=r.notification_date,@grp,@grp+1) as descGrp, 
    @prevdate:=r.notification_date as unused 

    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)
    order by r.notification_date DESC
) inR
where descGrp=2

QueryC 就是你的選擇。 因為它是兩個語句

  • 變量的初始化
  • 和大查詢字符串

...您需要按查詢的順序運行它們,每個查詢一個,按該順序運行,或者使用 PHP 多查詢並將兩者結合在一個調用中。 上一句中的那個鏈接是針對mysqli的,特此透露概念,做相應修改。

您可以簡單地使用下面的查詢來獲取表中的第二個最大日期或滿足您的要求:-

select joiningDate from t_member order by joiningDate desc limit 1,1

您可以使用ORDER BY DATE_COLUMN DESC LIMIT 1 OFFSET 1 我在查詢的末尾附加了這個代碼:

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
    )
ORDER BY r.notification_date DESC # Add this line
LIMIT 1 offset 1                  # and this line

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM