Yii关系子查询

时间:2017-05-31 18:33:44

标签: php mysql yii subquery

这是我的查询:

select * from order_shipping_date osd inner join  
(SELECT MAX(osd.id) as id FROM  order_shipping_date osd 
group by osd.order_id) osdi ON osd.id = osdi.id 

我保持这样做很好,但是希望能够将其定义为relations

稍后使用此类代码会更加舒适 这可行吗?我找不到任何例子。

1 个答案:

答案 0 :(得分:1)

Additional options可以在关系声明中指定。

public function relations()
{
    return array(
        'orderShippingDate' => array(
            // define you relation
            'join' => '(/* subquery here*/) osdi ON osdi.id=orderShippingDate.id',
            'joinType' => 'INNER JOIN'
        ),
    );
}