gpt4 book ai didi

php - 计算关系表条目

转载 作者:行者123 更新时间:2023-11-29 06:55:13 26 4
gpt4 key购买 nike

代码:

$criteria = new CDbCriteria;
$criteria->select='id, name, count(events.id) AS event_relational';
$criteria->with=array('events');
$model = Profiles::model()->findAll($criteria);

响应:

[{"id":"1","name":"Profile 1","event_relational":"0"}]

代码 2:

$criteria = new CDbCriteria;
$criteria->select='id, name';
$criteria->with=array('events');
$model = Profiles::model()->findAll($criteria);

响应 2:

[{"id":"1","name":"Profile 1","event_relational":null},{"id":"2","name":"Profile 2","event_relational":null}...]


当我使用 COUNT 时,查询在第一个 MySQL 条目中停止。我希望代码返回所有条目并检查是否有关系事件。

我想要的响应:

[{"id":"1","name":"Profile 1","event_relational":"0"},{"id":"2","name":"Profile 2","event_relational":"1"}...]

最佳答案

为此,您需要按 profile.id 进行分组:

$criteria = new CDbCriteria;
$criteria->select='id, name, count(events.id) AS event_relational';
$criteria->with=array('events');
$criteria->group='t.id';
$model = Profiles::model()->findAll($criteria);

关于php - 计算关系表条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13137654/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com