使用Yii查询构建器进行mysql查询

使用Yii查询构建器进行mysql查询

问题描述:

有什么方法可以在Yii查询构建器中编写下面的查询

Hi is there any way to write below query in Yii query builder

        SELECT t.email
        FROM otz_user_header t
        JOIN otz_customers r ON t.user_id = r.customer_user_id
        JOIN otz_customer_ratings cr ON cr.customer_user_id = r.customer_user_id
        WHERE r.rate_auto_approve =0
        AND r.rate_email_time IS NOT NULL
        AND r.total_rating_count IS NOT NULL
        AND cr.rating_date < CURDATE( )
        AND cr.rating_date >DATE_SUB( CURDATE( ) , INTERVAL 7
        DAY )

我已经使用Yii DAO方法编写了上面的查询,但是我正在寻找适合查询生成器的查询,这有可能吗?

i have written above query using Yii DAO method but i am looking to fit this query with query builder, is this possible ?

是的,可以. :-)哦,你的意思是,你怎么做到的? ;-)

Yep, sure is. :-) Oh, you mean, how do you do it? ;-)

从此处开始: http://www.yiiframework.com /doc/guide/1.1/en/database.query-builder

$emails = Yii::app()->db->createCommand()
    ->select('t.email')
    ->from('otz_user_header t')
    ->join('otz_customers r', 't.user_id = r.customer_user_id')
    … // some of this left out as an exercise for the reader
    ->where(
        array('and','r.rate_auto_approve=0'
            … // more left out here
            array('and',new CDbExpression('cr.rating_date < CURDATE()')),
        ),
    ),
    ->queryAll();

尚未对CDbExpression项目进行测试,但是其余项目应该可以正常工作.注意:查看有关语法的文档,这有些棘手.从本质上讲,每个AND/OR都将成为嵌套在原始where数组中的另一个数组行.

The CDbExpression item hasn't been tested, but the rest should work fine. Note: take a look at the documentation on where syntax, it gets a bit tricky. In essence, every AND/OR becomes another array line nested inside your original where array.