php - Mysql 查询对连接表进行排序

标签 php mysql sql zend-framework

我有一个 mysql 查询,我在排序发件人的最新消息时遇到问题 下面是我的查询:

   SELECT 
        `Mes`.`fromid`, 
        `Mes`.`is_read`, 
        `Mes`.`id` AS `mesid`, 
        `Mes`.`message`,
        max(Mes.date) AS `date`, 
        `User`.`username`, 
        `User`.`MemberID` AS `Uid` 
   FROM `messages` AS `Mes` 
   INNER JOIN `users` AS `User` ON User.MemberID = Mes.fromid 
   WHERE (Mes.toid = 5 AND Mes.fromid <> 5) 
   GROUP BY `Mes`.`fromid` 
   ORDER BY `date` DESC

这是我的数据库表: 用户表

MemberID      UserName     Email             Password
1             User1       user1@gmail.com   123456
2             User2       user2@gmail.com   123456
3             User3       user3@gmail.com   123456
4             User4       user4@gmail.com   123456
5             User5       user5@gmail.com   123456

消息表:

id      fromid     toid    message         is_read    date  
1       5          2       hello test 1    1          2012-08-24 01:00:00
2       2          5       hello test 2    1          2012-08-24 02:00:00
3       3          5       hello test 3    1          2012-08-24 03:00:00
4       4          5       hello test 4    1          2012-08-24 04:00:00
5       2          5       hello test 5    1          2012-08-25 05:00:00   

以及我的查询的输出:

SELECT 
     `Mes`.`fromid`, 
     `Mes`.`is_read`, 
     `Mes`.`id` AS `mesid`,
     `Mes`.`message`, 
     max(Mes.date) AS `date`, 
     `User`.`username`, 
     `User`.`MemberID` AS `Uid` 
FROM `messages` AS `Mes` 
INNER JOIN `users` AS `User` ON User.MemberID = Mes.fromid 
WHERE (Mes.toid = 5 AND Mes.fromid <> 5) 
GROUP BY `Mes`.`fromid` 
ORDER BY `date` DESC

是:

USERNAME     MESSAGE          DATE
user2        hello test 2     2012-08-25 05:00:00
user4        hello test 4     2012-08-25 04:00:00
user4        hello test 3     2012-08-25 03:00:00

如果您发现按日期排序是正确的,但最新消息不正确。我想要这样的输出。

USERNAME     MESSAGE          DATE
user2        hello test 5     2012-08-25 05:00:00
user4        hello test 4     2012-08-25 04:00:00
user4        hello test 3     2012-08-25 03:00:00

我想对最新消息进行排序,而不是消息“hello test 2” “你好测试 5

谁能帮我解决这个问题?

非常感谢...

最佳答案

应该是这样的

SELECT  *                              // -- select the columns you want
FROM    Messages a
            INNER JOIN
            (
                SELECT  fromid, toid, MAX(`date`) maxDATE
                FROM    Messages
                GROUP BY fromid, toid
            ) b ON a.fromID = b.fromID AND
                   a.toid = b.toid AND
                   a.`date` = b.maxDATE
            INNER JOIN users c
                ON c.MemberID = a.fromID
WHERE   a.toid = 5 AND a.fromid <> 5
ORDER BY `date` DESC

关于php - Mysql 查询对连接表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12291568/

相关文章:

php - 使用 PHP 时出现奇怪的语法错误 MySQL 错误

php - 警告 : filectime(): stat failed for path

php - 使用爆破PHP查询打印为空

mysql - SQL查询按缺失部分排序所有施工计划

java - SQL 更新语句 - 试图更新整个表错误

php - Doctrine 将一个实体发送给 DQL 的 NEW 构造函数

php - 用于管理显示哪些内容的模板系统?

mysql - 如果行中的值不在 SQL 的其他表中,如何使行中的值显示为 NULL

Mysql:从列表中查找大多数的所有者

sql - SUM 和按日期和 Material 分组