【问题标题】:How to make query joins in cakePHP?如何在 cakePHP 中进行查询连接?
【发布时间】:2012-07-28 23:44:33
【问题描述】:

我只是想问一下如何按照这个 SQL 语法在 CakePHP 中进行查询连接

SELECT a.id, SUM( r.jumlah_realisasi) AS jumlah_realisasi, SUM(b.jumlah_budget) AS jumlah_budget, SUM(c.jumlah_contrapos)
FROM accountposts a
LEFT JOIN
(
SELECT accountpost_id, agency_id, SUM(budget_after_changes) AS jumlah_budget
FROM budgets
GROUP BY accountpost_id
) b
ON a.id = b.accountpost_id
LEFT JOIN
(
SELECT accountpost_id, agency_id, SUM(realisation_value) AS jumlah_realisasi
FROM realisations
GROUP BY accountpost_id
) r
ON a.id = r.accountpost_id
LEFT JOIN
(
SELECT accountpost_id, agency_id, SUM(contrapos_value) AS jumlah_contrapos
FROM contraposts
GROUP BY accountpost_id
) c
ON a.id = c.accountpost_id
GROUP BY
a.id

我尝试了这种语法(我使用的是 CakePHP 2.x):

$joins = array(
               array(
                  'table' => 'budgets',
                  'alias' => 'Budget',
                  'type' => 'LEFT',
                  'conditions' => array('Accountpost.id = Budget.accountpost_id')
                ),
                array(
                  'table' => 'realisations',
                  'alias' => 'Realisation',
                  'type' => 'LEFT',
                  'conditions' => array('Accountpost.id = Realisation.accountpost_id')
                ),
                array(
                   'table' => 'contraposts',
                   'alias' => 'Contrapost',
                   'type' => 'LEFT',
                   'conditions' => array('Accountpost.id = Contrapost.accountpost_id')
                ),
         );

        $this->paginate = array(
            'limit' => 60,
            'joins' => $joins,
            'fields' => array('Accountpost.id','Accountpost.explanation','Accountpost.account_code',
                              'SUM(Budget.budget_after_changes) AS jumlah_budget','SUM(Realisation.realisation_value) AS jumlah_realisasi','SUM(Contrapost.contrapos_value) AS jumlah_contrapos'),
            'group' => array('Accountpost.id'),
            'order' => array('Accountpost.id' => 'ASC'),
        );

这是来自 CakePHP 的 SQL 转储:

SELECT `Accountpost`.`id`, `Accountpost`.`explanation`, `Accountpost`.`account_code`, SUM(`Budget`.`budget_after_changes`), `Budget`.`budget_after_changes`, `Realisation`.`realisation_value`, `Contrapost`.`contrapos_value` FROM `realisasi_anggaran`.`accountposts` AS `Accountpost` LEFT JOIN `realisasi_anggaran`.`budgets` AS `Budget` ON (`Accountpost`.`id` = `Budget`.`accountpost_id`) LEFT JOIN `realisasi_anggaran`.`realisations` AS `Realisation` ON (`Accountpost`.`id` = `Realisation`.`accountpost_id`) LEFT JOIN `realisasi_anggaran`.`contraposts` AS `Contrapost` ON (`Accountpost`.`id` = `Contrapost`.`accountpost_id`) WHERE 1 = 1 GROUP BY `Accountpost`.`id` ORDER BY `Accountpost`.`id` ASC LIMIT 60

但是SQL语法版本和CakePHP版本的结果不同,SQL语法查SUM时没有重复值,而CakePHP版本查SUM时有重复值。如何以正确的方式实现我的 SQL 语法到 cakePHP?

【问题讨论】:

  • 您可能希望减少已发布的代码。如果你这样做,你很可能会得到答案。

标签: php join model cakephp-2.1


【解决方案1】:

您的$options 数组构造不正确。这样,数组就有两个嵌套的命名键 joins,Cake 不会构造正确的 SQL 连接。

数组应如下所示:

$joins = array(
  array(
    'table' => 'budgets',
    'alias' => 'Budget',
    'type' => 'LEFT',
    'conditions' => array('Accountpost.id = Budget.accountpost_id')
  )
);

$this->paginate = array(
  'limit' => 60,
  'joins' => $joins,
  'fields' => array('Accountpost.id','Accountpost.explanation','Accountpost.account_code',
                                  'SUM(Budget.budget_after_changes) AS jumlah_budget'),
  'group' => array('Accountpost.id'),
  'order' => array('Accountpost.id' => 'ASC'),
);

或者像下面这样构造:

$options = array(
  'limit' => 60,
  'fields' => array('Accountpost.id','Accountpost.explanation','Accountpost.account_code',
                                  'SUM(Budget.budget_after_changes) AS jumlah_budget'),
  'group' => array('Accountpost.id'),
  'order' => array('Accountpost.id' => 'ASC'),
);

$options['joins'] = array(
  array(
    'table' => 'budgets',
    'alias' => 'Budget',
    'type' => 'LEFT',
    'conditions' => array('Accountpost.id = Budget.accountpost_id')
  )
);

$this->paginate = $options;

【讨论】:

  • 感谢您的帮助,它的作品。但是我在尝试加入多个表时遇到问题,检查 SUM 时仍然存在重复值。
  • 我不确定您现在如何调用联接以及重复值是什么。
  • 对不起,如果我的解释让您感到困惑。好的,我再次尝试解释,当我加入 Table Accountposts & Budgets 时,一切正常。但是,当我尝试同时加入 Table Accountposts、Budget、Realisations、Contraposts 并按照您上面解释的方式构建时。检查 SUM(Budget.budget_after_changes)、SUM(Realisation.realisation_value)、SUM(Contrapost.contrapos_value) 时存在重复值。
  • 这可能值得提出自己的问题,因为它不再是严格地在 Cake 中创建连接,或者您可以使用更新的连接代码和生成的 SQL 来更新您的问题。
  • 你在尝试实现子查询吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
  • 2022-01-14
相关资源
最近更新 更多