从相关表中检索 SUM(值)

发布于 2024-12-17 13:34:17 字数 369 浏览 1 评论 0原文

我有一个数据库表,比如说“人”,每个人都有存储在“投票”表中的选票。

每个人都会有很多票,其中一些是赞成票,一些是反对票,所以我对 COUNT 不感兴趣。

我有以下查询来检索单个人的选票:

$votes = $this->Vote->query("
  SELECT IFNULL( SUM( value ) , 0 ) AS vote_count
  FROM `votes`
  WHERE person_id = {$person_id}"); 

我是否可以在每次调用时获取一个人的此信息

$this->person->find("all")

I have a database table, lets say Person and each person has votes stored in a Vote table.

Each person will have many votes and some of these are up and some of these are down votes so I am not interested in COUNT.

I have the following query to retrieve votes for a single person:

$votes = $this->Vote->query("
  SELECT IFNULL( SUM( value ) , 0 ) AS vote_count
  FROM `votes`
  WHERE person_id = {$person_id}"); 

Is it possible for me to fetch this information for a Person each time I call

$this->person->find("all")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追风人 2024-12-24 13:34:17

这可以使用 $virtualFields 来完成:

在投票模型中,我添加以下虚拟字段字段:

var $virtualFields = array(
    'vote_count' => 'IFNULL( SUM( Vote.value ) , 0 )'
);

在 Person 模型中,我每次都使用以下 $hasMany 关系检索此字段:

var $hasMany = array('Vote' => array('fields' => array('vote_count')));

请注意,默认情况下,它将始终检索 Vote 表中的每个字段,这将导致通过以下方式检索“vote_count”SUM 字段:默认情况下,如果您要完整写出查询,则只会按预期显示一行。

如果您想检索所有行并删除 SUM 虚拟字段,那么您需要指定字段数组中除“vote_count”字段之外的所有字段(根据需要),例如

var $hasMany = array('Vote' => array('fields' => array('id', 'value')));

我建议通过使用几个字段来更好地控制这一点Person 模型中的函数本身,这将允许您即时决定如何检索选票:

function allWithVoteCounts() {
    $this->bindModel(array('hasMany' => 
        array('Vote' => array(
            'fields' => array('vote_count')
            )
        )
    ));
    return $this->find('all');
}

function allVotes() {
    $this->bindModel(array('hasMany' => 
        array('Vote' => array(
            'fields' => array('id', 'user_id', 'person_id', 'value', 'date')
            )
        )
    ));
    return $this->find('all');
}

然后在控制器中您可以调用:

$persons = $this->Person->allVotes();

$persons = $this->Person->allWithVoteCounts();

This can be done using $virtualFields:

In the Vote model I add the following virtual field:

var $virtualFields = array(
    'vote_count' => 'IFNULL( SUM( Vote.value ) , 0 )'
);

In the Person model I retrieve this every time by using the following $hasMany relationship:

var $hasMany = array('Vote' => array('fields' => array('vote_count')));

Note that by default it will always retrieve every field in the Vote table, this will result in retrieving the 'vote_count' SUM field by default and will only show one row as expected if you were to write out the query in full.

If you want to retrieve all of the rows and remove the SUM virtual field then you need to specify all the fields (as necessary) in the fields array except the 'vote_count' field e.g.

var $hasMany = array('Vote' => array('fields' => array('id', 'value')));

I would suggest this is better controlled by having a couple of functions in the Person model as such, which will allow you to decide on the fly how you want to retrieve the votes:

function allWithVoteCounts() {
    $this->bindModel(array('hasMany' => 
        array('Vote' => array(
            'fields' => array('vote_count')
            )
        )
    ));
    return $this->find('all');
}

function allVotes() {
    $this->bindModel(array('hasMany' => 
        array('Vote' => array(
            'fields' => array('id', 'user_id', 'person_id', 'value', 'date')
            )
        )
    ));
    return $this->find('all');
}

Then in the controller you can call:

$persons = $this->Person->allVotes();

or

$persons = $this->Person->allWithVoteCounts();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文