douzhan1031 2013-09-15 14:02
浏览 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.

    评论

报告相同问题?

悬赏问题

  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置