sharke Posted December 22, 2009 Share Posted December 22, 2009 Hi guys, i have function for count items from sub-categories but i need SUM from all sub-categories. There is 233 items <?php $result = mysql_query("SELECT *, (SELECT COUNT(galerija.kat_id) FROM galerija WHERE kat_id = kategori.kat_id) as cnt FROM kategori WHERE kat_parent = 1"); $total = mysql_fetch_assoc($result); echo $total['cnt']; ?> In this case i get result: 0 how can get correct result (233) items ? Link to comment https://forums.phpfreaks.com/topic/186054-count-items-from-sub-categories/ Share on other sites More sharing options...
fenway Posted December 22, 2009 Share Posted December 22, 2009 I don't see a column alias ANYWHERE. Link to comment https://forums.phpfreaks.com/topic/186054-count-items-from-sub-categories/#findComment-982718 Share on other sites More sharing options...
sharke Posted December 22, 2009 Author Share Posted December 22, 2009 soryy let me example, there is two tables (galerija and kategori) table kategori: kat_id kat_parent kat_naslov kat_datum table galerija: pic_id kat_id and others columns... If i use pasted code count only pictures from sub-categoryes, actually i need SUM all pictures from sub-categories. Similar like picture (233) items. soryy for bad english Link to comment https://forums.phpfreaks.com/topic/186054-count-items-from-sub-categories/#findComment-982744 Share on other sites More sharing options...
fenway Posted December 23, 2009 Share Posted December 23, 2009 I don't need an example -- you need to make an column alias called "cnt", or there won't be one. Link to comment https://forums.phpfreaks.com/topic/186054-count-items-from-sub-categories/#findComment-982763 Share on other sites More sharing options...
sharke Posted December 23, 2009 Author Share Posted December 23, 2009 this code i tested on phpmyadmin SELECT *, (SELECT COUNT(galerija.kat_id) FROM galerija WHERE kat_id = kategori.kat_id) as cnt FROM kategori WHERE kat_parent = 1; Ok, then example with code how can i get SUM if i make column CNT or whatever ? Link to comment https://forums.phpfreaks.com/topic/186054-count-items-from-sub-categories/#findComment-982772 Share on other sites More sharing options...
fenway Posted December 23, 2009 Share Posted December 23, 2009 Sigh.... SELECT *, (SELECT COUNT(galerija.kat_id) AS cnt FROM galerija WHERE kat_id = kategori.kat_id) as cnt FROM kategori WHERE kat_parent = 1; Link to comment https://forums.phpfreaks.com/topic/186054-count-items-from-sub-categories/#findComment-982775 Share on other sites More sharing options...
sharke Posted December 23, 2009 Author Share Posted December 23, 2009 soryy but here no SUM Category: - Test (kat_parent=0) - (kat_id=1) --- test1 (kat_parent=1) - total photos = 126 --- test1 (kat_parent=1) - total photos = 62 ================================ Total results on category Test = 188, i need this one Table galerija columns: pic_id kat_id etc.... Link to comment https://forums.phpfreaks.com/topic/186054-count-items-from-sub-categories/#findComment-982779 Share on other sites More sharing options...
fenway Posted December 23, 2009 Share Posted December 23, 2009 That's what PHP is for. Link to comment https://forums.phpfreaks.com/topic/186054-count-items-from-sub-categories/#findComment-982786 Share on other sites More sharing options...
sharke Posted December 23, 2009 Author Share Posted December 23, 2009 Why you can't add SUM function for all subcategoris on existing code, also here we have how many photos have in subcategory, but not SUM for Category. SELECT *, (SELECT COUNT(galerija.kat_id) FROM galerija WHERE kat_id = kategori.kat_id) as cnt FROM kategori WHERE kat_parent = 1; Link to comment https://forums.phpfreaks.com/topic/186054-count-items-from-sub-categories/#findComment-982791 Share on other sites More sharing options...
fenway Posted December 23, 2009 Share Posted December 23, 2009 I don't understand... you're already necessarily iterating through all of the results, why make mysql do more work? Link to comment https://forums.phpfreaks.com/topic/186054-count-items-from-sub-categories/#findComment-983363 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.