how to convert mysql subquery in yii CDbCriteria?(如何在 yii CDbCriteria 中转换 mysql 子查询?)
本文介绍了如何在 yii CDbCriteria 中转换 mysql 子查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将mysql子查询转换为yii CDbCriteria
表列表:
1) 用户组
2) user_group_data
3) user_group_data_revision
how to convert mysql subquery to yii CDbCriteria
table list:
1) user_group
2) user_group_data
3) user_group_data_revision
select * from (
select
ugd.group_id as group_id,
ug.user_id as user_id,
ugd.id as group_data_id,
ugd.group_name as group_name,
ugd.group_description as group_description,
ugd.email as group_admin_email,
ugd.image as group_image,
ugd.group_type as group_type,
ugd.request_type as group_request_type,
ugdr.revision_version as revision_version,
ugdr.admin_approve as revision_approve_status,
ugdr.publish as revision_publish,
ugdr.created_at as revision_created_at
from user_group_data_revision ugdr
left join user_group_data ugd on ugdr.group_data_id = ugd.id
left join user_group ug on ugdr.group_id = ug.id
order by `revision_version` desc
) rgd group by rgd.group_id
推荐答案
这里有一个小例子(如果我正确理解了结构/关系):
Here a small example(if I correctly understood structure/relations):
//Model UserGroupDataRevision
class UserGroupDataRevision extends CActiveRecord
{
public function relations()
{
return array(
'userGroup' => array(self::BELONGS_TO, 'UserGroup', 'group_id'),
'userGroupData' => array(self::BELONGS_TO, 'UserGroupData', 'group_data_id'),
);
}
//....
}
//model UserGroup
class UserGroup extends CActiveRecord {...}
//model UserGroupData
class UserGroupData extends CActiveRecord {...}
标准:
$criteria = new CDbCriteria();
$criteria->select = 'ugdr.revision_version'; // other fields
$criteria->alias = 'ugdr';
$criteria->with = array(
'userGroup' => array(
'alias' => 'ug',
'together' => true,
'select' => array('ug.user_id'), // other fields
),
'userGroupData' => array(
'alias'=> 'ugd',
'together' => true,
'select'=>array('ugd.group_id, ugd.group_name'), // other fields
),
);
$criteria->group = 'rgd.group_id';
$criteria->order = 'rgd.group_id'; // or something else
数据检查:
$dataProvider = new CActiveDataProvider('UserGroupDataRevision', array(
'criteria' => $criteria,
)
);
var_dump($dataProvider->getData());
die();
这篇关于如何在 yii CDbCriteria 中转换 mysql 子查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何在 yii CDbCriteria 中转换 mysql 子查询?
猜你喜欢
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- SQL 临时表问题 2022-01-01