4

如何将mysql查询转换为yii。?

我有 3 张桌子

  1. 用户头
  2. 顾客
  3. 客户评级

这是我的 sql 查询

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 ?

提前致谢。

4

2 回答 2

10

“itachi”答案是正确的,但如果你正在寻找 activerecord 方式......

型号:用户头

关系:

'activeCustomers' => array(
     self::HAS_MANY, 
     'Customer', 
     'customer_user_id', 
     'condition' => 'activeCustomers.rate_auto_approve=0 
                  AND activeCustomers.rate_email_time IS NOT NULL 
                  AND activeCustomers.total_rating_count IS NOT NULL'
),

型号:客户

关系:

'lastWeekRatings' => array(
     self::HAS_MANY, 
     'CustomerRating', 
     'customer_user_id', 
     'condition' => 'lastWeekRatings.rating_date < CURDATE() 
        AND lastWeekRatings.rating_date > DATE_SUB( CURDATE(), INTERVAL 7 DAY )'
),

下面的代码返回与您的查询相同的模型对象。(我没有测试过)

$useremails = UserHeader::model()
     ->with('activeCustomers', 'activeCustomers.lastWeekRatings')
     ->findAll(array(
           'select' => 't.email'
      ));
print_r($useremails);
于 2012-09-24T06:42:27.117 回答
5

USE DAO(数据访问对象

模型


public function test(){
        $sql = "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 )";

        return Yii::app()->db->createCommand($sql)->queryAll();
            }
于 2012-09-24T06:31:44.583 回答