douzhan1031 2013-09-15 14:02 采纳率: 0%
浏览 37

用where子句在其他表中的列的总和排序

I am using codeigniter and I want to list campaigns order_by how many donations each has received. So we are dealing with two tables, "campaigns" and "donations".

I used this

$this->db->select('campaigns.id as id, campaigns.userId as userId, name, amount');
$this->db->join('donations','donations.campaignId = campaigns.id', 'left');
$this->db->order_by("SUM(amount)", "DESC");

I want it to get all campaigns whether they have any donations or not and order by sum of the donations they have received. This query is getting just one campaign. Ideas?

  • 写回答

2条回答 默认 最新

  • dtrj74376 2013-09-15 14:51
    关注

    You need a group by campaigns.id and to change the select to have sum(amount) as amount.

    This is a SQL problem. I'm not intimately familiar with code-igniter so I won't attempt to write the code.

    评论

报告相同问题?