简体   繁体   English

cakephp 3(My)SQL聚合函数max()语法错误,靠近'AS MAX(`users`__`date_time`)AS`date`

[英]cakephp 3 (My)SQL aggregate function max() syntax error near 'AS MAX(`users`__`date_time`) AS `date`

I'm developing a social application with cakephp 3. 我正在使用cakephp 3开发社交应用程序。

I'm working on the messanging function right now. 我正在处理即时消息传递功能。 It works this way, that on the left side one gets displayed the users, who one has conversations with and in the middle is the conversation. 它的工作方式是,在左侧显示用户,与之进行对话的用户,中间是对话。

I want the users to be sorted by last messaging date. 我希望用户按上次消息传递日期排序。 Meaning, if Peter was the last one I wrote to, then Peter should appear on top and so on. 意思是,如果彼得是我写的最后一封信,那么彼得应该出现在顶部,依此类推。

I have the following query: 我有以下查询:

$query = $this->Messages
    ->find('all', [
        'fields' => [
            'MAX(`messages`.`max_date`)',
            'id',
            'sender_id',
            'reciever_id',
        ]
    ])
    ->where([
        'sender_id = ' => $active_user_id
    ])
    ->orWhere([
        'reciever_id = ' => $active_user_id
    ])
    ->group([
        'sender_id',
        'reciever_id'
    ])
    ->order([
        'id' => 'DESC'
    ]);

For performance reasons I added a grouping by sender_id and reciever_id, since I don't want to recieve all messages, but only find the users, that I wrote with so far. 出于性能原因,我添加了一个由sender_id和reciever_id组成的分组,因为我不想接收所有消息,而只能找到到目前为止编写的用户。

The problem is that the group-statement destroys the sorting. 问题在于组语句破坏了排序。 So I thought about a max-aggregate-function to preserve the order. 因此,我考虑了保留订单的max-aggregate-function。

However I get the following error-message by the cakephp framework: 但是,我通过cakephp框架收到以下错误消息:

Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; 错误:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误。 check the manual that corresponds to your MySQL server version for the right syntax to use near 'MAX(`messages`__`max_date`), Messages.id AS `Messages__id`, Messages.sender_id A' at line 1 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的MAX(`messages`__`max_date`),Messages.id AS`Messages__id,Messages.sender_id A'附近使用

So does anyone know, what is wrong with my query? 谁知道,我的查询出了什么问题? I gave my best to follow the example in the documentation. 我尽力遵循文档中的示例。

Why does it say messages __ max_date and not messages . 为什么说messages __ max_date而不是messages max_date ? max_date

That's simply not how you define computed fields and correspondig alias, this needs to be done in a key => value fashion, ie 这根本不是定义计算字段和对应别名的方式,这需要以key => value方式完成,即

'fields' => [
    'max_date' => 'MAX(`Messages`.`date_time`)',
    // ...
]

which would result in an SQL fragment like 这将导致像

MAX(`Messages`.`date_time`) AS `max_date`

See Cookbook > Database Access & ORM > Query Builder > Selecting Data 参见食谱>数据库访问和ORM>查询生成器>选择数据

Also as a tip, you might want to use expressions instead of simple strings, so that CakePHP can properly create platform specific SQL in case required. 另外,作为提示,您可能希望使用表达式而不是简单的字符串,以便CakePHP在需要的情况下可以正确创建平台特定的SQL。 MAX isn't affected AFAIR, so this is just a general hint. MAX不会影响AFAIR,因此这只是一个一般提示。

$query = $this->Messages->find();
$query
    ->select([
        'max_date' => $query->func()->max($this->Messages->aliasField('date_time'))
        // ...
    ])
    // ...

See Cookbook > Database Access & ORM > Query Builder > Using SQL Functions 参见食谱>数据库访问和ORM>查询生成器>使用SQL函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM