0

i am working on a Cakephp 2.x i am newbie to cakephp and getting a very hard time to grasp the cake query methods i have implemented a join query but the distinct is not working here is my code

what actually i am trying to accomplish :

i have a table name Messages in which there are duplicate numbers in field Message.mobileNo ... i just want to pick the distinct numbers and then on the bases of those distinct numbers i am comparing or joining the contacts table in which there are mobileNo,workNo etc fields and checking that wether those numbers are in the contacts table or not .. if they are grab the contact name for me ..hope you understand it

so what i did is ... i am trying to get the rows of contacts table against the distnct numbers of Messages table..

function getRecentMessages($userid){
    $this->bindModel(array(
        'belongsTo' => array(
            'Contact' => array(
                'className' => 'Contact',
                'foreignKey' => false,
                'conditions' => array(



                    'AND' =>
                    array(
                        array('OR' => array(
                            array('Message.mobileNo = Contact.mobileNo'),
                            array('Message.mobileNo = Contact.workNo'),
                            array('Message.mobileNo = Contact.homeNo'),
                            array('Message.mobileNo = Contact.other'),
                        )),


                    )


                ),
                'type' => 'LEFT',
                'order'=>'Message.idTextMessage DESC',


            )
        )
    ), false);

    return $this->find('all', array('conditions' => array('Message.User_id' => $userid),
            'contain' => array('Contact' ),
        'fields' => array(' DISTINCT Message.mobileNo',//distnct not working 
            'Contact.mobileNo',
            'Contact.workNo',
            'Contact.homeNo',
            'Contact.other',
            'Contact.name',
            'Message.dateTime',
            'Message.type',
            'Message.body'),

        'limit' => 6));


}

i have tried this

DISTINCT (Message.mobileNo) as mobileNo

but didnt work too

i have tried

     'group' => 'Message.mobileNo',   

by using this it works but then the query is not getting the required results .. i mean the DESC condition not works then

4

1 回答 1

3

DISTINCT在 SQL Server 中适用于从结果集中返回的整行。因此,如果您的数据包含,例如:

mobileNo workNo homeNo name     ... 
1234     1234   5678   Joe      ...
1234     867    5309   Stacy    ...  
1        555    5555   Sentinel ...         

DISTINCT不会删除 Joe 或 Stacy,因为整个行不相等。另一方面,如果您的数据包含:

mobileNo workNo homeNo name     ... 
1234     1234   5678   Joe      ...
1234     1234   5678   Joe      ...
1234     1234   5678   Joe      ...
1234     1234   5678   Joe      ...
1234     1234   5678   Joe      ...
1234     867    5309   Stacy    ...  
1        555    5555   Sentinel ...

您的查询将只返回一个名为“Joe”的条目,因为它只返回结果集中的不同行。

你应该知道 aDISTINCT通常是我认为 SQL 中的反模式。通常是 SQL 的证据已被修改以产生作者认为正确的结果,但查询并不能真正代表他们的意图。

所以我不得不问你,你在查询中添加 DISTINCT 的目标是什么?或者,更好的是,想象一下如果您被要求只返回mobileNo下表中的不同值:

mobileNo workNo homeNo name     ... 
1234     1234   5678   Joe      ...
1234     867    5309   Stacy    ...  
1        555    5555   Sentinel ...         

如果你是一个 SQL 数据库,你不会返回哪一行?你会拒绝史黛西还是乔?这是您DISTINCT查询的根本问题 - 数据库无法知道您要排除哪一行。根据您提供的信息,它不能只返回 distinct mobileNos,因为具有不同名称但相同mobileNo的两行都同样有效地被排除在外。

XY 问题

所以,问问自己,你真正想解决什么问题?我怀疑你在这里的帖子是所谓的 XY 问题的一个典型例子。这是我遇到过的这个问题的最佳示例的链接,我在下面复制了它:

它是什么?

XY 问题是询问您尝试的解决方案,而不是您的实际问题。

也就是说,您正在尝试解决问题X,并且您认为解决方案Y会起作用,但是X您没有询问何时遇到麻烦,而是询问Y.

问题

这可能会导致试图帮助您解决问题的人感到沮丧,因为当您提出问题时,您需要帮助的解决方案可能与您尝试解决的问题没有任何明显的联系。

如何避免

为避免落入此陷阱,请始终包含有关更广泛情况的信息以及任何尝试的解决方案。如果有人询问更多信息,或者特别是更具体的问题,请提供详细信息。如果您认为有其他解决方案会被建议并且您已经排除,那么不要试图避免再次重复它们 - 而是说明您排除它们的原因,因为这会提供有关您的要求的更多信息并帮助他人提供更好的答案。

一个例子

最近的 IRC 对话用于说明:


<Q> 有没有返回两个分隔符之间的字符串的函数?

<B> 我不明白你的意思,但我怀疑已经有一个函数

<C> 分割和切片

<D> 分区太

<Q> 我尝试了分区
<Q> 我试图使用内置函数在字符串“attribute1:50.223,attribute2:442.1”中获取类似这样的数字

<D> 为什么不只解析字符串呢?

<Q> 我认为可能有一些内置的解析东西

<D> 对 = [x.strip() for x in s.split(",")]; attribs = {k: v for x in pairs for k, v in x.split(": ")}
<D> 有一些库,但简单的格式很容易——如果你不关心错误处理
< D> 如果可能,最好将源更改为使用众所周知的格式,例如 json 或 yaml

<Q> 这段代码实际上来自 HTML
<Q> 但我不知道如何使用 HTMLParser 或其他任何名称解析 Javascript

<D> 它只是嵌入在 html 中,还是 html 的某些损坏版本?

<Q> 它嵌入在 HTML 中

<D> 如果它是javascript(也就是说,除了缺少外大括号),json 可能可以解析它

<问>谢谢

<D> 没明说:json只解析数据结构,不解析js代码

<Q> 我只需要解析一个数据结构


问题实际上是关于如何解析 Javascript 数据结构,而不是找到“两个分隔符之间的字符串”,但要解决真正的问题需要相当多的时间和直觉。

这在完全互动的聊天中更容易做到(不管是什么模式),但是在 SE 网站上,你稍微润色一下帖子,发布它,然后有 5-30 分钟或更长时间,在反馈之前,它真的有助于从一开始就朝着正确的方向前进。

我相信根据您的查询,您实际上并没有试图找到mobileNo数据库的不同 s,因为您在查询中包含了所有这些其他数据。那么你真正想要完成的是什么?

于 2013-07-20T23:58:08.677 回答