CakePHP和子查询

时间:2011-04-24 10:29:53

标签: sql cakephp

如何使用cake语法编写SQL子查询? 我知道如何编写简单的查询,但我无法处理子查询。

这是原始查询:

SELECT Assumption.id, Referee.id, Referee.first_name, Referee.second_name
FROM referees AS Referee
INNER JOIN (

    SELECT a.id, a.referee_id
    FROM assumptions a
    WHERE a.season_id =7
) AS Assumption ON Referee.id = Assumption.referee_id

3 个答案:

答案 0 :(得分:6)

由于您不理解语法,因此这是实际查询:

$records = $this->Referee->find('all', array(
                'fields' => array(
                    'Assumption.id', 'Referee.id', 'Referee.first_name', 'Referee.second_name'
                    ),
                'joins' => array(
                    array(
                        'table' => 'assumptions',
                        'alias' => 'Assumption',
                        'type'  => 'INNER',
                        'foreignKey'    => false,
                        'conditions'    => array('Referee.id = Assumption.referee_id', 'Assumption.season_id = 7'),
                        ),
                ),
            )
        );

产生此查询:

SELECT 
    `Assumption`.`id`,
    `Referee`.`id`,
    `Referee`.`first_name`,
    `Referee`.`second_name`
FROM `referees` AS `Referee`
INNER JOIN assumptions AS `Assumption`
    ON (`Referee`.`id` = `Assumption`.`referee_id` 
        AND `Assumption`.`season_id` = 7)

提供您正在寻找的结果。

示例输出:

Array
(
    [0] => Array
        (
            [Assumption] => Array
                (
                    [id] => 1
                    [0] => Array
                        (
                            [id] => 1
                            [season_id] => 7
                            [referee_id] => 1
                            [name] => SomeAssumpton
                        )

                )

            [Referee] => Array
                (
                    [id] => 1
                    [first_name] => Ref
                    [second_name] => one
                )

        )

)

答案 1 :(得分:2)

cdburgess的答案对于这种情况是正确的;它以最简单,最直接的方式解决问题。

话虽如此,在解决执行子查询的一般性问题时,Cookbook会记录preferred solution to the subquery problem。它不是很优雅,但它是导游说的方式。

或者您可以非常小心,只需执行$ this-> Model-> query()。

答案 2 :(得分:1)

所以看似几年(几个小时)并阅读蛋糕来源......如果你想简单地将你的子查询输入你的蛋糕条件......

它使用php stdClass而不是数组条目......它只会将你的“值”转储到查询中......

    $subquery = new stdClass();
    $subquery->type = "expression";
    $subquery->value = "Product.id IN (select product_id from product_categories where category_id='$category_id' or belongs_to='$category_id')";

    $options['conditions'][] = $subquery;  <- dump the class into your conditions array!

做正常查询 $ this-&gt; table-&gt; find('all',$ options)

示例:(带有子查询quickfix的普通蛋糕)

    //only from my vendor
    $options['conditions']['collection_id'] = $vendor_id;

    //inner join to CollectionProduct
    $options['joins'][0] = array(
        "table" => "collection_products",
        "alias" => "CollectionProduct",
        "type"  => "INNER",
        "conditions" => array(
            "Product.id = CollectionProduct.product_id",
        ),
    );

    //show only from current category
    if ($category_id) {
        $subquery = new stdClass();
        $subquery->type = "expression";
        $subquery->value = "Product.id IN (select product_id from product_categories where category_id='$category_id' or belongs_to='$category_id')";
        $options['conditions'][] = $subquery;
    } else {
        //get 18 random items... no category selected?
        $options['limit'] = 18;
    }

    return $this->find('all', $options);