0

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?

4

2 回答 2

0

您需要 agroup by campaigns.id并将 select 更改为 have sum(amount) as amount

这是一个 SQL 问题。我对代码点火器不是很熟悉,所以我不会尝试编写代码。

于 2013-09-15T14:51:48.090 回答
0

我认为您正在尝试在每个活动的捐赠表中获得捐赠的总和。如果我是对的,查询应该如下所示。

SELECT campaigns.*, sum(donation.donation) as donation_received FROM `donation`,`campaigns` WHERE campaigns.id=donation.cam_id group by donation.cam_id order by sum(donation.donation) desc

如果它对你有用,我可以为你在 codeignter 中编写它。请让我知道状态。

于 2013-09-15T19:05:33.233 回答