Select users that has an unanswered support ticket

אVי :

I have 2 tables:

Users:

id      name                 phone  
 __________________________________
1       Zusha                123a  
2       Zelig                123b  
3       Shmerel              123e  

support_messages:

id      userId           fromPhone    toPhone     date
 ________________________________________________________
1       1                123a          support     2020-03-01 19:15:29
2       1                support       123a        2020-03-01 20:35:08
3       2                123c          support     2020-03-02 19:15:23
4       1                123a          support     2020-03-03 19:15:56
5       3                123e          support     2020-03-04 19:17:22 
6       3                support       123e        2020-03-04 19:18:34
7       3                123e          support     2020-03-04 19:19:24 
8       4                support       123e        2020-03-04 19:25:42

I want to select all users that have an unanswered chat (that in the last chat record of the user, 'fromPhone' != '' support) and I need to add the date of the last 2 chats (that were sent by the user - 'fromPhone' != '' support) to the result.

For example:

The expected results for the example should be:

user 1 sent 2 messages to support and the last one wasn't answered yet

user 2 sent only one chat to support so the 'preLastChatDate' is empty

user 3 got a response from support so he isn't listed

id      name                 phone    preLastChatDate         lastChatDate 
 _____________________________________________________________________________________
1       Zusha                123a     2020-03-01 19:15:29     2020-03-03 19:15:56
2       Zelig                123b                             2020-03-02 19:15:23

I have the following code:

select
   `u`.`name` AS `name`,
   `last`.`date` AS `lastChatDate`,
   `u`.`id` AS `id`,
   `u`.`phone` AS `phone`
from
   (
      `user` `u` ,
         (
            select
               `support_messages`.`userId` AS `userId`,
               `support_messages`.`fromNumber` AS `fromNumber`,
               `support_messages`.`date` AS `date` 
            from
               `support_messages` 
            where
               `support_messages`.`id` in 
               (
                  select
                     max(`support_messages`.`id`) 
                  from
                     `support_messages` 
                  group by
                     `support_messages`.`userId` 
               )
         )
         `last` 
   )
where
   (
(`u`.`id` = `last`.`userId`) 
      and 
      (
         `last`.`fromNumber` <> 'support' 
      )
   )
ORDER BY
   `u`.`id` DESC

This gives me everything I need but NOT the 'preLastChatDate'.

How can I and the 'preLastChatDate' to my results (and keep empty if there isn't any)?

I'm using Mysql version: 5.7.22-log Thanks

PeterHe :

For mysql 5.7 or earlier:

SELECT  u.`name` AS `name`,
   l.`date` AS `lastChatDate`,
   u.`id` AS `id`,
   u.`phone` AS `phone`,
   (SELECT MAX(p.`date`) FROM `support_messages` p WHERE p.userId = l.userId AND p.`date`<l.`date`) AS preLastChatDate
FROM (
  select
   m.`userId` AS `userId`,
   MAX(m.`date`) AS `date` 
  from
     `support_messages` m
  group by m.`userId`) l
INNER JOIN `support_messages` lm
ON lm.`userId`=l.`userId`
AND lm.`date`=l.`date`
INNER JOIN `user` u
ON u.id=l.`userId`
WHERE lm.`fromNumber` <> 'support'

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=32808&siteId=1