MYSQL最大状态值

问题描述 投票:0回答:1

我有以下表格,记录了为他们采取的字母与操作的详细信息。

信表

+-----------+-------------+
| letter_id | description |
+-----------+-------------+
|         1 | A           |
|         2 | B           |
|         3 | C           |
|         4 | D           |
+-----------+-------------+

行动表

+-----------+--------+------------+---------------+
| action_id | ref_no |    date    | action_status |
+-----------+--------+------------+---------------+
|         1 |      1 | 2018-09-20 | On-Going      |
|         2 |      1 | 2018-09-22 | Finished      |
|         3 |      3 | 2018-09-16 | On-Going      |
|         4 |      4 | 2018-09-26 | On-Going      |
|         5 |      4 | 2018-09-27 | Finished      |
+-----------+--------+------------+---------------+

并需要获得以下输出

+-----------+-------------+------------+---------------+
| letter_id | description |    date    | action_status |
+-----------+-------------+------------+---------------+
|         1 | A           | 2018-09-22 | Finished      |
|         2 | B           | -          | Pending       |
|         3 | C           | 2018-09-16 | On-Going      |
|         4 | D           | 2018-09-27 | Finished      |
+-----------+-------------+------------+---------------+

我使用了以下查询

select letter.letter_id,letter.description, action.date, action.action_status
            from letter
           left join action on letter.letter_id=action.ref_no
            where (date in     
                    (          
                        select max(date) from action   
                            where letter.letter_id=action.ref_no 
                    ))

但是上面的查询会生成以下输出

+-----------+-------------+------------+---------------+
| letter_id | description |    date    | action_status |
+-----------+-------------+------------+---------------+
|         1 | A           | 2018-09-20 | On-Going      |
|         1 | A           | 2018-09-22 | Finished      |
|         2 | B           | -          | Pending       |
|         3 | C           | 2018-09-16 | On-Going      |
|         4 | D           | 2018-09-26 | On-Going      |
|         4 | D           | 2018-09-27 | Finished      |
+-----------+-------------+------------+---------------+

我无法理解我的错误。谁能帮我 ?

mysql
1个回答
0
投票
DROP TABLE IF EXISTS action;

CREATE TABLE action
(action_id SERIAL PRIMARY KEY
,letter_id INT NOT NULL
,date DATE NOT NULL
,action_status VARCHAR(20) NOT NULL
);

INSERT INTO action VALUES
(1,101,'2018-09-20','On-Going'),
(2,101,'2018-09-22','Finished'),
(3,103,'2018-09-16','On-Going'),
(4,104,'2018-09-26','On-Going'),
(5,104,'2018-09-27','Finished');

SELECT x.* 
  FROM action x 
  JOIN 
     ( SELECT letter_id, MAX(date) max_date FROM action GROUP BY letter_id ) y 
    ON y.letter_id = x.letter_id 
   AND y.max_date = x.date;
+-----------+-----------+------------+---------------+
| action_id | letter_id | date       | action_status |
+-----------+-----------+------------+---------------+
|         2 |       101 | 2018-09-22 | Finished      |
|         3 |       103 | 2018-09-16 | On-Going      |
|         5 |       104 | 2018-09-27 | Finished      |
+-----------+-----------+------------+---------------+

据推测,你可以弄清楚剩下的

© www.soinside.com 2019 - 2024. All rights reserved.