mysql query with Yii query builder(使用 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 项目尚未经过测试,但其余的应该可以正常工作.注意:查看有关 where 语法的文档,它有点棘手.本质上,每个 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.
这篇关于使用 Yii 查询构建器的 mysql 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Yii 查询构建器的 mysql 查询


基础教程推荐
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01