通过不使用group by子句来排序

时间:2016-09-26 12:35:04

标签: 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 ) )

任何人都可以帮助我:(

3 个答案:

答案 0 :(得分:1)

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

正如group by clause上的MySQL文档所说(粗体是我的):

  

如果禁用ONLY_FULL_GROUP_BY,则为标准的MySQL扩展   GROUP BY的SQL使用允许选择列表,HAVING条件或   ORDER BY列表引用非聚合列,即使列   在功能上不依赖于GROUP BY列。这会导致MySQL   接受前面的查询。 在这种情况下,服务器是免费的   从每个组中选择任何值,所以除非它们相同,否则   选择的值是不确定的,这可能不是你想要的。   此外,不能从每个组中选择值   受添加ORDER BY子句的影响。发生结果集排序   选择值后,ORDER BY不会影响哪个   服务器选择的每个组中的值。

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

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

答案 1 :(得分:0)

此代码有效:

SELECT * FROM generate_invoice
WHERE id IN 
(
    SELECT max(id) as id
    FROM generate_invoice 
    GROUP by pay_id 
    ORDER by id DESC
)

答案 2 :(得分:0)

如何group by DESC order

尝试

 SELECT * FROM (  SELECT * FROM generate_invoice ORDER BY id DESC ) AS g GROUP BY g.pay_id

OR

使用此代码

SELECT m1.*,m2.* FROM generate_invoice m1 LEFT JOIN generate_invoice m2 ON (m1.pay_id = m2.pay_id AND m1.id < m2.id ) order by m1.id desc