Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL-查询未返回正确的结果_Mysql_Sql - Fatal编程技术网

MySQL-查询未返回正确的结果

MySQL-查询未返回正确的结果,mysql,sql,Mysql,Sql,小提琴: 在上面的小提琴中,我的查询返回了正确的数据,但当swipes表中的swp\u的多个记录中出现1时,它似乎消除了结果。起初我以为这是因为groupby swipes.swp_BY,但我删除了它,似乎这不是问题所在,所以我把它放了回去。运行查询时,您会看到当前查询返回的结果是swp\u id2、3和4,而不是5和6。它们被消除了,这是因为在swp\u by1中,在swp\u id2中发生了一次。我希望查询也返回5和6的结果 质疑 这是怎么回事? 我正在建立一个聊天系统,匹配的用户可以互相聊

小提琴:

在上面的小提琴中,我的查询返回了正确的数据,但当swipes表中的
swp\u的多个记录中出现1时,它似乎消除了结果。起初我以为这是因为
groupby swipes.swp_BY
,但我删除了它,似乎这不是问题所在,所以我把它放了回去。运行查询时,您会看到当前查询返回的结果是
swp\u id
2、3和4
,而不是
5和6
。它们被消除了,这是因为在
swp\u by
1
中,在
swp\u id
2
中发生了一次。我希望查询也返回
5和6
的结果

质疑

这是怎么回事?


我正在建立一个聊天系统,匹配的用户可以互相聊天<代码>滑动
存储匹配项,消息是用户之间的交易消息。通过此查询,我试图设置要与之聊天的匹配用户的主列表,点击/单击某个用户,然后出现聊天框(稍后将显示)。我想简单介绍一下这个项目可能有助于理解这个问题。

我在这里发布了我的asnwer,小提琴没有给出正确的链接

不,明白了


1和11之间的消息由group by缩减为1,消息也缩减为14和15。您可以在这里看到我在哪里扩展了group by,但我不明白您在寻找什么结果表会有所帮助imensly@nbk这并不是因为团体的原因。。我想你没有阅读我的完整描述(在问题上方)。我提到我曾试图通过删除该组,但没有任何效果。请检查一下。如果你完成后点击上面导航栏上的更新,小提琴会给出正确的链接。我已经把它弄出来了,所以现在的链接是正确的,好吗。。。所以所需要做的就是通过。。。伙计,你也可以在评论中说这么多。。无论如何。。。谢谢。。这解决了我的问题。。我感谢你为我抽出时间:)爱你的兄弟
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');
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
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