簡體   English   中英

MySQL - 查詢未返回正確結果

[英]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');

小提琴: DB FIDDLE

在上面的小提琴中,我的查詢返回了正確的數據,但是當 1 出現在swp_by在 swipes 表中的多個記錄中時,它似乎消除了結果。 起初我以為是因為GROUP BY swipes.swp_by但我刪除了它,看起來這不是問題所以我把它放回去了。 當您運行查詢時,您會看到當前查詢返回swp_id 2, 3 & 4但不返回5 & 6的結果。 它們被淘汰了,這是因為在swp_by 1中出現了swp_id 2一次。 我希望查詢也返回5 & 6的結果。

詢問

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

這是怎么回事?

我正在建立一個聊天系統,匹配的用戶可以互相聊天。 swipes存儲匹配項,消息是用戶之間的交易消息。 通過此查詢,我試圖設置匹配用戶的主頁列表,以便在您點擊/單擊用戶的位置聊天,然后出現聊天框(稍后會這樣做)。 我認為只是簡要介紹該項目可能有助於理解問題。

我在這里發布我的答案小提琴沒有給出正確的鏈接

不知道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

暫無
暫無

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

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