如何通过具有条件的关联模型字段对查询结果进行排序?

发布于 2024-10-19 22:08:55 字数 1171 浏览 2 评论 0原文

在过去的两天里,我一直在努力寻找这个问题的解决方案,因此任何可以提供解决方案或指向我需要知道的内容的链接的人都会给我一个巨大的帮助。

我在 CakePHP 中有以下模型关系,

Keyword hasMany Click

我想构建一个查询(通过 $this->Keyword->find 或使用自定义查询),该查询将返回按点击次数排序的相似关键字列表他们上周收到了。不幸的是,我不能只使用 counterCache 'click_count' 字段来执行此操作,因为我只需要计算上周发生的点击次数。为了使事情变得更加复杂,我还必须向关键字字段添加 LIKE() 条件。

这是我到目前为止所得到的:

$result = $this->Keyword->find('all',array(
        'conditions' => array(
            'word_count >' => $keyword['Keyword']['word_count'],
            'UPPER(keyword) LIKE' => "%".strtoupper($keyword['Keyword']['keyword'])."%"
        ),
        'recursive' => 0,
        'limit' => 10
    ));

我只需要添加一个位,根据关联的 Click 记录的数量对这些结果进行排序,其中 Click.created 是在上周内。我已经弄清楚该部分应该看起来像这样:

array(
        'conditions' => array(
            'Click.created >' => date("Y-m-d",strtotime("1 week ago"))
        ),
        'fields' => array(
            'COUNT(Click.keyword_id) as count'
        ),
        'group' => 'Click.keyword_id',
        'order' => 'count DESC'
    );

我只是不确定这部分应该去哪里。

请问有人可以让我摆脱痛苦吗?谢谢 :)

I've spent the last two days trying to find a solution to this problem so anyone that can either provide a solution or a link to somewhere I could find out what I need to know would be doing me a huge favour.

I've got the following model relationships in CakePHP

Keyword hasMany Click

I want to construct a query (either through $this->Keyword->find or by using a custom query) that will return a list of similar keywords ordered by the number of clicks they've received in the last week. Unfortunately I can't just use the counterCache 'click_count' field to do this because I need to only count clicks that occurred in the last week. To further complicate things I've got to add a LIKE() condition to the keyword field too.

Here is what I've got so far:

$result = $this->Keyword->find('all',array(
        'conditions' => array(
            'word_count >' => $keyword['Keyword']['word_count'],
            'UPPER(keyword) LIKE' => "%".strtoupper($keyword['Keyword']['keyword'])."%"
        ),
        'recursive' => 0,
        'limit' => 10
    ));

I just need to add the bit that sorts these results by the number of associated Click records where Click.created is within the last week. I've worked out that that part should look something like:

array(
        'conditions' => array(
            'Click.created >' => date("Y-m-d",strtotime("1 week ago"))
        ),
        'fields' => array(
            'COUNT(Click.keyword_id) as count'
        ),
        'group' => 'Click.keyword_id',
        'order' => 'count DESC'
    );

I'm just not sure where this bit should go.

Please could someone put me out of my misery? Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白龙吟 2024-10-26 22:08:56

好吧,我成功地在这个页面上写了一个 SQL 查询,它可以满足我的需要: http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/ASP/Q_20646898.html

这是它的样子:

SELECT *
        FROM keywords
        AS Keyword
        LEFT JOIN (SELECT keyword_id,count(*) AS click_count FROM clicks GROUP BY keyword_id)
        AS Click
        ON Keyword.id = Click.keyword_id
        WHERE Keyword.word_count > ".$keyword['Keyword']['word_count']."
        AND UPPER(Keyword.keyword) LIKE '%".strtoupper($keyword['Keyword']['keyword'])."%'
        ORDER BY Click.click_count DESC

希望其他人会发现这很有用。

Well, I managed to blag an SQL query that does what I needed from this page: http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/ASP/Q_20646898.html

Here is what it looks like:

SELECT *
        FROM keywords
        AS Keyword
        LEFT JOIN (SELECT keyword_id,count(*) AS click_count FROM clicks GROUP BY keyword_id)
        AS Click
        ON Keyword.id = Click.keyword_id
        WHERE Keyword.word_count > ".$keyword['Keyword']['word_count']."
        AND UPPER(Keyword.keyword) LIKE '%".strtoupper($keyword['Keyword']['keyword'])."%'
        ORDER BY Click.click_count DESC

Hopefully someone else will find this useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文