php - Order by 不与 group by 子句一起使用

标签 php mysql

我在cakephp中使用mysql的corequery。我想要按降序排列的记录。这是我的表结构enter image description here

enter code here
$coreQueryUser = $this->Message->query(
            "select * from messages where messages.list_id = 3 
            group By (if(sender_id > reciever_id,  sender_id, reciever_id)), 
                (if(sender_id > reciever_id,  reciever_id, sender_id))
            order by id desc
            "
        );

我想要属于列表 id 3 的最后一条消息(sender_id 和 reciver_id,反之亦然)

当我运行此查询时,我得到以下输出

<pre>Array
(
[0] => Array
    (
        [messages] => Array
            (
                [id] => 1
                [sender_id] => 21
                [reciever_id] => 10
                [list_id] => 3
                [message] => hello
                [add_date] => 2016-09-25 00:00:00
                [is_check] => 0
            )

    )

[1] => Array
    (
        [messages] => Array
            (
                [id] => 3
                [sender_id] => 22
                [reciever_id] => 10
                [list_id] => 3
                [message] => hello s
                [add_date] => 2016-09-25 16:39:41
                [is_check] => 0
            )

    )

)

但我想要这样的结果:

Array
(
    [0] => Array
        (
            [messages] => Array
                (
                    [id] => 2
                    [sender_id] => 10
                    [reciever_id] => 21
                    [list_id] => 3
                    [message] => hello sir
                    [add_date] => 2016-09-25 00:00:00
                    [is_check] => 0
                )

) [1] => Array ( [messages] => Array ( [id] => 6 [sender_id] => 22 [reciever_id] => 10 [list_id] => 3 [message] => new [add_date] => 2016-09-25 16:39:41 [is_check] => 0 ) )

)

谁能帮帮我:(

最佳答案

问题在于您的查询违反了 sql 标准,因为选择列表中有多个字段既不在分组依据列表中,也不属于聚合函数(例如 sum().遗憾的是,MySQL 允许在某些 sql 模式设置下运行此类无效查询(最新版本 MySQL 的默认设置将阻止此类查询运行)。

作为 MySQL 文档 group by clause说(粗体是我的):

If ONLY_FULL_GROUP_BY is disabled, a MySQL extension to the standard SQL use of GROUP BY permits the select list, HAVING condition, or ORDER BY list to refer to nonaggregated columns even if the columns are not functionally dependent on GROUP BY columns. This causes MySQL to accept the preceding query. In this case, the server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate, which is probably not what you want. Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause. Result set sorting occurs after values have been chosen, and ORDER BY does not affect which value within each group the server chooses.

您显然想要最新记录(每个组都有 max(id) )。正确的方法是使用一个子查询来返回每个组的 max(id)并在外部查询中使用 ids 连接回主表以获取其他字段的值:

select m.*
from
messages m
inner join (
            select max(id) as maxid
            from messages
            where messages.list_id = 3 
            group By (if(sender_id > reciever_id,  sender_id, reciever_id)), 
            (if(sender_id > reciever_id,  reciever_id, sender_id))
           ) t1 on m.id=t1.maxid

关于php - Order by 不与 group by 子句一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39702993/

相关文章:

PHP MYSQLI统计连续两个日期的用户

php - 无需使用 javascript 将 POST 页面加载到 &lt;iframe&gt;

mysql - 为什么我的 Perl 脚本无法连接到数据库?

php - 使用php计算数据库中用户的时差

php - mysql模式的mysqli版本

php - QuickSort:当 Pivot 交换位置时无法理解部分

PHPUnit 和 Mock 对象不起作用

带有 MSSQL 的 PHP 未安装在 centos 7 中

mysql - 具有条件的列中字段值的总和

c# - 如何将标签文本更改为 MySql 数据库中的最后一个值?