Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.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_Sorting_Subquery_Sql Order By - Fatal编程技术网

正确排序MySQL结果(可能需要子查询)

正确排序MySQL结果(可能需要子查询),mysql,sorting,subquery,sql-order-by,Mysql,Sorting,Subquery,Sql Order By,我想创建一个用户可以互相发送消息的消息系统。它应该像Facebook一样基于线程。因此,每个用户应该只与另一个用户在一个特定的消息线程中 所有消息的主要概述没有按我希望的那样工作。它只显示线程最早(第一个)的消息文本和日期,不按最新发送的消息排序 这是我的SELECT-语句,我想我需要一个子查询,让它按照最新的日期和文本进行排序,但我不熟悉,也不知道放在哪里以及如何排序 因此,基本上我只希望消息线程按照收到的最新消息进行排序,显示特定线程的最新文本和日期 $menu = mysql_qu

我想创建一个用户可以互相发送消息的消息系统。它应该像Facebook一样基于线程。因此,每个用户应该只与另一个用户在一个特定的消息线程中

所有消息的主要概述没有按我希望的那样工作。它只显示线程最早(第一个)的消息文本和日期,不按最新发送的消息排序

这是我的
SELECT
-语句,我想我需要一个子查询,让它按照最新的日期和文本进行排序,但我不熟悉,也不知道放在哪里以及如何排序

因此,基本上我只希望消息线程按照收到的最新消息进行排序,显示特定线程的最新文本和日期

    $menu = mysql_query("SELECT 
t.`id`, t.`creator_uid`,
c.`uid` AS content_uid, c.`text`, c.`date` AS content_date,
a.`status`, a.`date` AS assign_date,
CASE 
WHEN t.`creator_uid`='".sInput($_SESSION['uid'])."' THEN `receiver_uid`
WHEN t.`receiver_uid`='".sInput($_SESSION['uid'])."' THEN `creator_uid`
END AS uid
FROM `messages_content` c
LEFT JOIN `messages_thread` t ON t.`id` = c.`thread_id`
LEFT JOIN `messages_assign` a ON a.`id` = t.`id`
WHERE 
t.`creator_uid`='".sInput($_SESSION['uid'])."' 
OR 
t.`receiver_uid`='".sInput($_SESSION['uid'])."' 
GROUP BY t.`id` 
ORDER BY c.`id` DESC") or die (mysql_error());

MySQL数据库结构:

消息分配
id
uid
线程id
状态
日期

$menu = mysql_query("SELECT 
    t.`id`, t.`creator_uid`,
    c.`uid` AS content_uid, c.`text`, c.`date` AS content_date,
    a.`status`, a.`date` AS assign_date,
    CASE 
    WHEN t.`creator_uid`='".sInput($_SESSION['uid'])."' THEN `receiver_uid`
    WHEN t.`receiver_uid`='".sInput($_SESSION['uid'])."' THEN `creator_uid`
    END AS uid
FROM `messages_content` c
LEFT JOIN `messages_thread` t ON t.`id` = c.`thread_id`
LEFT JOIN `messages_assign` a ON a.`id` = t.`id`
LEFT JOIN (SELECT thread_id, MAX(date) AS latest_post_date
            FROM messages_contents
            GROUP BY thread_id) AS m ON t.id = m.thread_id
WHERE 
t.`creator_uid`='".sInput($_SESSION['uid'])."' 
OR 
t.`receiver_uid`='".sInput($_SESSION['uid'])."' 
GROUP BY t.`id` 
ORDER BY m.latest_post_date DESC") or die (mysql_error());
消息内容
id
uid
线程id
文本
日期

$menu = mysql_query("SELECT 
    t.`id`, t.`creator_uid`,
    c.`uid` AS content_uid, c.`text`, c.`date` AS content_date,
    a.`status`, a.`date` AS assign_date,
    CASE 
    WHEN t.`creator_uid`='".sInput($_SESSION['uid'])."' THEN `receiver_uid`
    WHEN t.`receiver_uid`='".sInput($_SESSION['uid'])."' THEN `creator_uid`
    END AS uid
FROM `messages_content` c
LEFT JOIN `messages_thread` t ON t.`id` = c.`thread_id`
LEFT JOIN `messages_assign` a ON a.`id` = t.`id`
LEFT JOIN (SELECT thread_id, MAX(date) AS latest_post_date
            FROM messages_contents
            GROUP BY thread_id) AS m ON t.id = m.thread_id
WHERE 
t.`creator_uid`='".sInput($_SESSION['uid'])."' 
OR 
t.`receiver_uid`='".sInput($_SESSION['uid'])."' 
GROUP BY t.`id` 
ORDER BY m.latest_post_date DESC") or die (mysql_error());

messages\u-thread
id
creator\u-uid
receiver\u-uid
,您需要加入一个子查询,该子查询可以:

SELECT select thread_id, MAX(date) as latest_post_date
FROM messages_contents
GROUP BY thread_id
然后按
latest\u post\u date

$menu = mysql_query("SELECT 
    t.`id`, t.`creator_uid`,
    c.`uid` AS content_uid, c.`text`, c.`date` AS content_date,
    a.`status`, a.`date` AS assign_date,
    CASE 
    WHEN t.`creator_uid`='".sInput($_SESSION['uid'])."' THEN `receiver_uid`
    WHEN t.`receiver_uid`='".sInput($_SESSION['uid'])."' THEN `creator_uid`
    END AS uid
FROM `messages_content` c
LEFT JOIN `messages_thread` t ON t.`id` = c.`thread_id`
LEFT JOIN `messages_assign` a ON a.`id` = t.`id`
LEFT JOIN (SELECT thread_id, MAX(date) AS latest_post_date
            FROM messages_contents
            GROUP BY thread_id) AS m ON t.id = m.thread_id
WHERE 
t.`creator_uid`='".sInput($_SESSION['uid'])."' 
OR 
t.`receiver_uid`='".sInput($_SESSION['uid'])."' 
GROUP BY t.`id` 
ORDER BY m.latest_post_date DESC") or die (mysql_error());

谢谢,它似乎可以正确地排序,但它不会显示消息线程的最新文本。我试图在子查询中添加
文本
,但是它根本没有显示任何文本。我是否需要另一个子查询以获取已发送的最新文本?我以为您只想在最新消息时间之前订购,而不是显示最新消息。有关如何获取整行的最新日期,请参见。