简体   繁体   中英

MySQL - Query not returning proper results

CREATE TABLE `swipes` (
  `swp_id` bigint(20) NOT NULL,
  `swp_by` bigint(20) NOT NULL,
  `swp_to` bigint(20) NOT NULL,
  `swp_type` varchar(255) NOT NULL,
  `swp_status` enum('requested','accepted','declined') NOT NULL,
  `swp_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `swipes` (`swp_id`, `swp_by`, `swp_to`, `swp_type`, `swp_status`, `swp_date`) VALUES
(1, 8, 11, 'top', 'accepted', '2020-04-18 20:48:45'),
(2, 1, 11, 'right', 'accepted', '2020-04-18 20:41:49'),
(3, 12, 1, 'right', 'accepted', '2020-04-18 20:41:49'),
(4, 13, 1, 'right', 'accepted', '2020-04-18 20:41:49'),
(5, 1, 14, 'right', 'accepted', '2020-04-18 20:41:49'),
(6, 1, 15, 'top', 'accepted', '2020-04-18 20:41:49');


CREATE TABLE `messages` (
  `msg_id` bigint(20) NOT NULL,
  `msg_from` bigint(20) NOT NULL,
  `msg_to` bigint(20) NOT NULL,
  `msg_message` longtext NOT NULL,
  `msg_seen` enum('yes','no') NOT NULL DEFAULT 'no',
  `msg_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `messages` (`msg_id`, `msg_from`, `msg_to`, `msg_message`, `msg_seen`, `msg_time`) VALUES
(1, 11, 1, 'How are you?', 'yes', '2020-04-14 21:01:05'),
(3, 1, 11, 'I am fine.. you?', 'no', '2020-04-14 20:54:07'),
(4, 1, 8, 'How are you?', 'yes', '2020-04-14 21:01:05'),
(5, 8, 1, 'I am good... You say?', 'yes', '2020-04-14 21:13:34'),
(6, 1, 11, 'Thik hun... Tum batao..', 'yes', '2020-04-14 21:16:05'),
(7, 11, 1, 'Okay', 'yes', '2020-04-16 09:16:39'),
(8, 8, 1, 'Yes, it\'s a good idea.', 'yes', '2020-04-16 09:16:39'),
(9, 1, 8, 'Thought so.. Would you like to join?', 'yes', '2020-04-16 09:23:39'),
(10, 8, 1, 'Are you there?', 'yes', '2020-04-23 11:57:39'),
(12, 8, 1, 'Would you like to join?', 'yes', '2020-04-23 10:42:27'),
(13, 1, 11, 'We will arrange things for you :)', 'yes', '2020-04-23 10:59:04');

Fiddle: DB FIDDLE

In the fiddle above my query is returning correct data but it seems to eliminate results when 1 is present in multiple records of swp_by in swipes table. At first I thought it was because of GROUP BY swipes.swp_by but I removed it and it seemed like it wasn't the issue so I placed it back. When you run the query you see that currently the query returns result for swp_id 2, 3 & 4 but not for 5 & 6 . They are eliminated and it's because in swp_by 1 occurred in swp_id 2 once. I want the query to return the results for 5 & 6 as well.

Query

SELECT 
    swp_id,
    swp_by,
    swp_to,
    msg_from,
    msg_to,
    msg_message,
    GREATEST(MAX(msg_time), swipes.swp_date) AS msgdate,
    COUNT(msg_id) AS msgcnt
FROM
    swipes
        LEFT JOIN
    (SELECT 
        *
    FROM
        messages
    ORDER BY msg_time DESC) messages ON ((messages.msg_from = swipes.swp_by
        AND messages.msg_to = swipes.swp_to)
        OR (messages.msg_from = swipes.swp_to
        AND messages.msg_to = swipes.swp_by))
WHERE
    (swipes.swp_by = 1 OR swipes.swp_to = 1)
        AND swipes.swp_status = 'accepted'
GROUP BY swipes.swp_by
ORDER BY GREATEST(MAX(messages.msg_time),
        MAX(swipes.swp_date)) DESC

What is this all about?

I am setting up a chatting system and users who are matched are able chat with each other. swipes stores the matches and messages are the transacted messages between the users. With this query I am trying to set up the home list of matched users to chat with where you tap/click a user and the chat box appears (will make that later). I thought just giving a brief about the project might help in understanding the problem.

I post my asnwer here the fiddle doesn't give the right link

no got it https://www.db-fiddle.com/f/2yKt6d5RWngXVYJKPGZL6m/2

SELECT 
    swp_id,
    swp_by,
    swp_to,
    msg_from,
    msg_to,
    msg_message ,
    GREATEST(MAX(msg_time), swipes.swp_date) AS msgdate,
    COUNT(msg_id) AS msgcnt
FROM
    swipes
        LEFT JOIN
    (SELECT 
        *
    FROM
        messages
    ORDER BY msg_time DESC) messages 
    ON (messages.msg_from = swipes.swp_by
        AND messages.msg_to = swipes.swp_to)
        OR 
        (messages.msg_from = swipes.swp_to
        AND messages.msg_to = swipes.swp_by)
WHERE
    (swipes.swp_by = 1 OR swipes.swp_to = 1)
        AND swipes.swp_status = 'accepted'
GROUP BY swipes.swp_by,swipes.swp_to
ORDER BY GREATEST(MAX(messages.msg_time),
        MAX(swipes.swp_date)) DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM