MySQL使用group by检索JOIN中的最后一条记录

时间:2017-09-21 12:20:31

标签: mysql join group-by

我有以下数据表:

1.Table follow_up as:

mysql> select * from follow_up;
+--------------+----------------+--------------------------------------------------+-------------------+---------+---------------+-----------+---------------+----------+
| follow_up_id | feedback_close | feedback_open                                    | is_email_required | is_Open | reminder_date | client_id | conclusion_id | stage_id |
+--------------+----------------+--------------------------------------------------+-------------------+---------+---------------+-----------+---------------+----------+
|            1 | NULL           | dsffsdfsdfsd                                     |                 1 |       1 | 2017-09-20    |       101 |            96 |       72 |
|            2 | NULL           | FSGDFHFGHFG                                      |                 1 |       1 | 2017-09-28    |       101 |           251 |       72 |
|            3 | NULL           | Tender stage fb                                  |                 0 |       1 | NULL          |       101 |            98 |      163 |
|            4 | NULL           | Call back tender stage update date from 28 to 30 |                 1 |       1 | 2017-09-28    |       101 |            96 |      163 |
|            5 | NULL           | Metting follow up for next meeting               |                 1 |       1 | 2017-10-02    |       101 |            96 |       73 |
+--------------+----------------+--------------------------------------------------+-------------------+---------+---------------+-----------+---------------+----------+

2。表logs表示为:

mysql> SELECT *  from logs where transaction = 'FLWUP';
+---------+---------+---------------------+---------+-------------+
| user_id | menu_id | logs_time           | tran_id | transaction |
+---------+---------+---------------------+---------+-------------+
|      84 |      69 | 2017-09-19 19:31:04 |       1 | FLWUP       |
|      84 |      69 | 2017-09-19 19:31:25 |       2 | FLWUP       |
|      84 |      69 | 2017-09-20 19:10:41 |       2 | FLWUP       |
|      84 |      69 | 2017-09-21 12:35:01 |       3 | FLWUP       |
|      84 |      69 | 2017-09-21 12:35:26 |       4 | FLWUP       |
|      84 |      69 | 2017-09-21 12:36:16 |       4 | FLWUP       |
|      84 |      69 | 2017-09-21 12:38:30 |       5 | FLWUP       |
+---------+---------+---------------------+---------+-------------+
7 rows in set (0.00 sec)                                           

第3。表allcode表格为:

mysql> select * from allcode where code_type like 'MARK%';
+------------------+---------+------+----------------------+
| code_type        | code_id | srno | code_name            |
+------------------+---------+------+----------------------+
| MARKETING_STAGES |      72 |    1 | Enquiry              |
| MARKETING_STAGES |      73 |    3 | Meeting              |
| MARKETING_STAGES |      74 |    4 | Presentation         |
| MARKETING_STAGES |     163 |    2 | Tender               |
+------------------+---------+------+----------------------+
11 rows in set (0.00 sec)

我调用了一个查询并得到了结果:

mysql> select f.follow_up_id,f.feedback_open, f.feedback_close, f.reminder_date, 
ast.code_name as stage, ac.code_name as conclusion, max(l.logs_time)  
from follow_up f 
join logs l on l.tran_id = f.follow_up_id 
join allcode ast on ast.code_id = f.stage_id 
join allcode ac on ac.code_id = f.conclusion_id 
where l.transaction='FLWUP' and f.client_id = 101 
group by ast.code_name order by ast.srno;
+--------------+------------------------------------+----------------+---------------+---------+------------+---------------------+
| follow_up_id | feedback_open                      | feedback_close | reminder_date | stage   | conclusion | max(l.logs_time)    |
+--------------+------------------------------------+----------------+---------------+---------+------------+---------------------+
|            1 | dsffsdfsdfsd                       | NULL           | 2017-09-20    | Enquiry | Call Back  | 2017-09-20 19:10:41 |
|            3 | Tender stage fb                    | NULL           | NULL          | Tender  | Next       | 2017-09-21 12:36:16 |
|            5 | Metting follow up for next meeting | NULL           | 2017-10-02    | Meeting | Call Back  | 2017-09-21 12:38:30 |
+--------------+------------------------------------+----------------+---------------+---------+------------+---------------------+
3 rows in set (0.00 sec)

但我希望结果为:

+--------------+-----------------------------------------------------+----------------+---------------+---------+------------+---------------------+
| follow_up_id | feedback_open                                       | feedback_close | reminder_date | stage   | conclusion | max(l.logs_time)    |
+--------------+-----------------------------------------------------+----------------+---------------+---------+------------+---------------------+
|            2 | FSGDFHFGHFG                                         | NULL           | 2017-09-20    | Enquiry | Call Back  | 2017-09-20 19:10:41 |
|            4 | Call back tender stage update date from 28 to 30    | NULL           | NULL          | Tender  | Next       | 2017-09-21 12:36:16 |
|            5 | Metting follow up for next meeting                  | NULL           | 2017-10-02    | Meeting | Call Back  | 2017-09-21 12:38:30 |
+--------------+-----------------------------------------------------+----------------+---------------+---------+------------+---------------------+
3 rows in set (0.00 sec)

我无法加入并分组以获得所需的结果。

conclusion_id

stage_idfollow_up指的是表code_id的{​​{1}}。

问题:
我想要的结果是

  1. 分组 allcode
  2. stage_id
  3. 排序 srno
  4. allcode表的最后/最近follow_up_id

2 个答案:

答案 0 :(得分:1)

DEMO包括我的答案,根据需要提供完整组的原始问题,以及Reupal在演示中的答案。您在结果ID中缺少示例数据中的值,因此我只是根据ID创建它们(现在更新为ISO,回调但缺少98.)

我的结果与您的结果不符;但我相信你的预期结果是错误的。

当多个stage_ID存在时,好像你想要每个stage_ID的最大follow_up_ID

这可以通过派生表/内联视图来处理,获取由stage_ID分组的最大follow_UP_ID并将其连接回您的集合。将结果限制为仅包括stage_Id的最大follow_Up_ID。

我也不是mySQL的扩展组的粉丝,并且更喜欢包括未在组中的select中聚合的所有列。使用扩展组倾向于隐藏潜在问题。在这种情况下,仅通过ast.code_name进行分组允许引擎从其他列中选择非不同的值。您最终没有获得所需的结果,而且它隐藏了您在查询中获得多条记录的事实,如果不是通过使用/误用扩展组。

SELECT f.follow_up_id,f.feedback_open, f.feedback_close, f.reminder_date, 
ast.code_name as stage, ac.code_name as conclusion, max(l.logs_time)  
from follow_up f 
join logs l on l.tran_id = f.follow_up_id 
join allcode ast on ast.code_id = f.stage_id 
join allcode ac on ac.code_id = f.conclusion_id 
JOIN SELECT max(follow_up_ID) MFID, stage_ID 
      FROM follow_up 
      GROUP BY stage_ID) Z 
  on f.follow_up_ID = Z.MFID 
 and F.Stage_ID = Z.Stage_ID
WHERE l.transaction='FLWUP' and f.client_id = 101 
GROUP BY f.follow_up_id,f.feedback_open, f.feedback_close, f.reminder_date, 
ast.code_name , ac.code_name
ORDER BY ast.srno;

答案 1 :(得分:0)

请尝试以下操作,注意顺序和按顺序分组。

select f.follow_up_id,f.feedback_open, f.feedback_close, f.reminder_date, 
ast.code_name as stage, ac.code_name as conclusion, max(l.logs_time)  
from follow_up f 
join logs l on l.tran_id = f.follow_up_id 
join allcode ast on ast.code_id = f.stage_id 
join allcode ac on ac.code_id = f.conclusion_id 
where l.transaction='FLWUP' and f.client_id = 101 
group by follow_up.stage_id order by ast.srno, follow_up.follow_up_id DESC;

这应该有效,如果不是,那么你应该搜索如何在多列上设置排序。

参考。 article- SQL multiple column ordering