CakePHP找到最近的日期

时间:2014-02-03 19:04:48

标签: php mysql sql cakephp select

编辑:我明白了。检查底部是否有我的解决方案。

我正在尝试查询MOST RECENT activity.date不在过去30天和未来30天之间的所有行。换句话说,如果他们在上个月有活动,请不要选择它们。这是现在发送给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',
            )
        )

我尝试过做MAX(Activity.date),但根本没有运气。我有点认为它与连接有关。由于可以有许多与单个customer_id相关联的活动,我认为它只是加入它可以找到的第一个活动行。我不知道如何制作它以便它选择最近的活动.date。

Cake版本是2.4.5。

谢谢,非常感谢任何帮助。

解决方案:

'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'
            )
        ),

1 个答案:

答案 0 :(得分:0)

解决方案:

'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'
        )
    ),