2

I can't find here: http://framework.zend.com/manual/en/zend.db.select.html any reference to SUM using mysql.

How can we perform a simple:

SELECT SUM(mark) as total_mark FROM `student`

Using Zend_Db_Table_Abstract ?

Tried:

this->select(SUM ('myintcolumn'))->from('mytable');

No luck. :(

Thanks, MEM

Charles
  • 50,943
  • 13
  • 104
  • 142
MEM
  • 30,529
  • 42
  • 121
  • 191

3 Answers3

3

This is another less hardcoded way:

$this->select()->from($this, new Zend_Db_Expr("SUM(myintcolumn)"));
Mahomedalid
  • 3,074
  • 2
  • 17
  • 13
3

did you try

this->select()->from('mytable', array('sum(myintcolumn) as sum'));
Raj
  • 22,346
  • 14
  • 99
  • 142
aletzo
  • 2,471
  • 1
  • 27
  • 31
  • oh... like putting a query inside the select() method... I thought on that, but I was wonder, if I do that, why do I need the Zend db anyway? Just asking... – MEM Nov 24 '10 at 17:54
  • zend db will help a lot. For example it prevents SQL injection – aletzo Nov 24 '10 at 18:00
  • Same as PDO usage so I believe.Anyway. :) No luck. :( It seems that the "sum" alias is not assumed. I get: exception 'Zend_Db_Table_Row_Exception' with message 'Specified column "sum" is not in the row - But I believe I need to use the alias to grab the sum as a row. :( – MEM Nov 24 '10 at 18:02
  • Thanks. Where did you find this information? I mean, the correct way to do the sintax? :? – MEM Nov 24 '10 at 18:12
  • The expresions needs to be included in a Zend_Db_Expr, the alias like an array. ej. $this->select()->from($this, array('alias' => new Zend_Db_Expr("SUM(myintcolumn)"))); – Mahomedalid May 23 '12 at 16:03
0

$select->from($this->_name, array('mytable'=> new Zend_Db_Expr('SUM(myintcolumn)')));

  • This is quite an old answer which has an accepted answer, I'm not sure how much this "answer" really adds. Your answer should also should have an explanation of why this might be better than other answers if you feel they are inadequate. – Sam Jenkins Apr 23 '13 at 11:50