简体   繁体   English

CakePHP查找最新日期

[英]CakePHP find most recent date

EDIT: I figured it out. 编辑:我想通了。 Check at the bottom for my solution. 检查底部的解决方案。

I'm trying to query for all rows where the MOST RECENT activity.date is NOT between 30 days in the past AND 30 days in the future. 我正在尝试查询MOST RECENT activity.date不在过去30天到未来30天之间的所有行。 In other words, don't select them if they have an activity in the last month. 换句话说,如果他们上个月有活动,请不要选择他们。 Here's the array being sent to find() as it is right now: 这是当前发送到find()的数组:

'Declined' => array(
            'conditions' => array(
                'Program.deal_status' => 'declined',
                "Program.date_submitted > DATE_SUB(CURDATE(), INTERVAL 60 DAY)",
                'Activity.date NOT BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) and DATE_ADD(CURDATE(), INTERVAL 30 DAY)',
            ),
            'joins' => array(
                array(
                    'table' => 'programs',
                    'alias' => 'Program',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Program.customer_id = Customer.customer_id'
                    )
                ),
                array(
                    'table' => 'activities',
                    'alias' => 'Activity',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Activity.customer_id = Customer.customer_id',
                    )
                ),
            ),
            'order' => array(
                'Program.date_submitted DESC',

            ),
            'group' => array(
                'Customer.customer_id',
            ),
            'fields' => array(
                'Customer.customer_id',
                "CONCAT_WS(' ', CustomerPersonalInformation.first_name, CustomerPersonalInformation.last_name) AS full_name",
                'CustomerContactInformation.email',
                'Program.date_submitted',
                'Program.underwriters_notes',
                'Activity.date',
            )
        )

I've tried doing MAX(Activity.date), but no luck at all. 我试过做MAX(Activity.date),但根本没有运气。 I'm sorta thinking it's got something to do with the joins. 我有点认为这与联接有关。 Since there can be many activities associated with a single customer_id, I think it's just joining the first activity row it can find. 由于一个单一的customer_id可能有许多活动,所以我认为它只是加入它可以找到的第一个活动行。 I'm not sure how to make it so that it's picking the MOST RECENT activity.date. 我不确定如何制作它,以便选择最近的activity.date。

Cake version is 2.4.5. 蛋糕版本是2.4.5。

Thanks, any help is much appreciated. 谢谢,非常感谢您的帮助。

SOLUTION: 解:

'Declined' => array(
            'conditions' => array(
                'Program.deal_status' => 'declined',
                "Customer.sales_associate {CONDITION}",
                "Program.date_submitted > DATE_SUB(CURDATE(), INTERVAL 60 DAY)",
                'Customer.store {STORE_CONDITION}',
            ),
            'joins' => array(
                array(
                    'table' => 'programs',
                    'alias' => 'Program',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Program.customer_id = Customer.customer_id'
                    )
                ),
                array(
                    'table' => 'activities',
                    'alias' => 'Activity',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Activity.customer_id = Customer.customer_id',
                    )
                ),
            ),
            'order' => array(
                'Program.date_submitted DESC',
                "MAX(Activity.date) ASC"
            ),
            'group' => array(
                'Activity.customer_id HAVING most_recent NOT BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) and DATE_ADD(NOW(), INTERVAL 30 DAY)'
            ),
            'fields' => array(
                'customer_id' => 'Customer.customer_id',
                'Customer' => "CONCAT_WS(' ', CustomerPersonalInformation.first_name, CustomerPersonalInformation.last_name) AS full_name",
                'Email' => 'CustomerContactInformation.email',
                'Program Date' => 'Program.date_submitted',
                'Underwriter notes' => 'Program.underwriters_notes',
                'Last Activity Date' => 'Activity.date',
                'hidden' => 'MAX(Activity.date) AS most_recent'
            )
        ),

SOLUTION: 解:

'Declined' => array(
        'conditions' => array(
            'Program.deal_status' => 'declined',
            "Customer.sales_associate {CONDITION}",
            "Program.date_submitted > DATE_SUB(CURDATE(), INTERVAL 60 DAY)",
            'Customer.store {STORE_CONDITION}',
        ),
        'joins' => array(
            array(
                'table' => 'programs',
                'alias' => 'Program',
                'type' => 'LEFT',
                'conditions' => array(
                    'Program.customer_id = Customer.customer_id'
                )
            ),
            array(
                'table' => 'activities',
                'alias' => 'Activity',
                'type' => 'LEFT',
                'conditions' => array(
                    'Activity.customer_id = Customer.customer_id',
                )
            ),
        ),
        'order' => array(
            'Program.date_submitted DESC',
            "MAX(Activity.date) ASC"
        ),
        // HAVE TO GROUP BY THE DATE
        'group' => array(
            'Activity.customer_id HAVING most_recent NOT BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) and DATE_ADD(NOW(), INTERVAL 30 DAY)'
        ),
        'fields' => array(
            'customer_id' => 'Customer.customer_id',
            'Customer' => "CONCAT_WS(' ', CustomerPersonalInformation.first_name, CustomerPersonalInformation.last_name) AS full_name",
            'Email' => 'CustomerContactInformation.email',
            'Program Date' => 'Program.date_submitted',
            'Underwriter notes' => 'Program.underwriters_notes',
            'Last Activity Date' => 'Activity.date',
            'hidden' => 'MAX(Activity.date) AS most_recent'
        )
    ),

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

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