CakePHP 3 - 如何在查询中使用左连接

时间:2016-05-03 12:12:49

标签: mysql left-join cakephp-3.0

我有以下查询可行,但在与客户交谈后,我意识到我需要离开联接才能获得所需的结果而且我不知道该怎么做。

这是我的疑问:

$query = $this->AssetStatusAudits->find('all', [
                'contain' => [
                    'AssetStatus',
                    'Users',
                    'Assets' => function ($q) {
                        return $q->contain([
                            'Employees',
                            'Sites'
                        ]);
                    }
                ],
                'conditions' => [
                    'AssetStatusAudits.created >' => $from,
                    'AssetStatusAudits.created <' => $to
                ]
            ]);

            $query->select([
                'createdDate' => 'date(AssetStatusAudits.created)',
                'assetStatus' => 'AssetStatus.desc_en',
                'firstName' => 'Users.first_name',
                'lastName' => 'Users.last_name',
                'count' => $query->func()
                    ->count('*')
            ])
                ->group('date(AssetStatusAudits.created)', 'assetStatus.desc_en', 'users.last_name')
                ->order('Users.last_name', 'Users.created');

以上查询在蛋糕中生成:

SELECT date(AssetStatusAudits.created) AS `createdDate`, 
AssetStatus.desc_en AS `assetStatus`, 
Users.first_name AS `firstName`, 
Users.last_name AS `lastName`, 
(COUNT(*)) AS `count` 
FROM asset_status_audits AssetStatusAudits 
INNER JOIN asset_status AssetStatus ON AssetStatus.id = (AssetStatusAudits.asset_status_id) 
INNER JOIN users Users ON Users.id = (AssetStatusAudits.user_id) 
INNER JOIN assets Assets ON Assets.id = (AssetStatusAudits.asset_id) 
LEFT JOIN employees Employees ON Employees.id = (Assets.employee_id) 
INNER JOIN sites Sites ON Sites.id = (Assets.site_id) 
WHERE (AssetStatusAudits.created > date('2016-01-13') AND AssetStatusAudits.created < date('2016-05-03')) 
GROUP BY date(AssetStatusAudits.created)  
ORDER BY Users.last_name

但这是我需要添加到cakePHP的查询,我不知道该怎么做:

SELECT 
date(asset_tracking.asset_status_audits.created) as 'Date',
asset_tracking.asset_status.desc_en as 'Status',
asset_tracking.users.first_name as 'First Name' ,
asset_tracking.users.last_name as 'Last Name',
count(*)
FROM asset_tracking.asset_status_audits
LEFT OUTER JOIN asset_tracking.assets ON asset_tracking.asset_status_audits.asset_id = asset_tracking.assets.id
LEFT OUTER JOIN asset_tracking.asset_status ON asset_tracking.asset_status_audits.asset_status_id = asset_tracking.asset_status.id
LEFT OUTER JOIN asset_tracking.users ON asset_tracking.asset_status_audits.user_id = asset_tracking.users.id
LEFT OUTER JOIN asset_tracking.employees ON asset_tracking.assets.employee_id = asset_tracking.employees.id 
LEFT OUTER JOIN asset_tracking.sites ON asset_tracking.assets.site_id = asset_tracking.sites.id
WHERE asset_tracking.asset_status_audits.created between date('2016-01-13') and date('2016-05-19')
group by
date(asset_tracking.asset_status_audits.created) ,
asset_tracking.asset_status.desc_en,
asset_tracking.users.last_name
ORDER BY asset_tracking.users.last_name, asset_tracking.users.created;

0 个答案:

没有答案