mikenl Posted August 7, 2007 Share Posted August 7, 2007 I need to know the number of categories (cat_id). I have tried to use COUNT but cannot get it to work... Please assist if you know how to do it? $lookup = " SELECT prostorage_roh_eingang.id as subcat_id, prostorage_roh_eingang.charge, prostorage_roh_eingang.lieferant, prostorage_roh_eingang.qty_kg, prostorage_roh_eingang.qty_l, prostorage_charge.charge as cat_id, prostorage_charge.date, prostorage_roh.id, prostorage_roh.name, prostorage_roh.abb FROM prostorage_charge JOIN prostorage_roh_eingang JOIN prostorage_roh ON prostorage_roh_eingang.charge = prostorage_charge.charge WHERE prostorage_roh_eingang.id = prostorage_roh.id AND prostorage_charge.date < '$today' ORDER BY prostorage_roh_eingang.charge "; $query = mysql_query($lookup); $num_rows = mysql_num_rows($query); $current_catid = ''; while ($row = mysql_fetch_assoc($query)) { if ($current_catid != $row['cat_id']) { // Close previous subcat table if not 1st time through if ($current_catid != '') { echo '</div>'; } //First entry for this category - show category header $current_catid = $row['cat_id']; } // End if //Display the subcat data } //End while loop Link to comment https://forums.phpfreaks.com/topic/63705-count/ Share on other sites More sharing options...
NArc0t1c Posted August 7, 2007 Share Posted August 7, 2007 $query = mysql_query("SELECT * FROM table"); $get_rows = mysql_num_rows($query); echo 'There is ' . $get_rows . ' rows in table.'; Link to comment https://forums.phpfreaks.com/topic/63705-count/#findComment-317458 Share on other sites More sharing options...
mikenl Posted August 7, 2007 Author Share Posted August 7, 2007 THX but I don't need to know the rows in the table, I only need to know the number in one column 'prostorage_charge.charge as cat_id' Link to comment https://forums.phpfreaks.com/topic/63705-count/#findComment-317495 Share on other sites More sharing options...
fenway Posted August 10, 2007 Share Posted August 10, 2007 You're trying to do what, exactly? Link to comment https://forums.phpfreaks.com/topic/63705-count/#findComment-320247 Share on other sites More sharing options...
mikenl Posted August 10, 2007 Author Share Posted August 10, 2007 Hi, I want to know the number of found rows for prostorage_charge.charge as cat_id, see the code. So, not the total number of rows returned, that is not the problem. Only the number of rows that were found for prostorage_charge.charge as cat_id. I need this because there are apparently 4 categories (prostorage_charge.charge as cat_id) returned by this query, and 5 subcategories (prostorage_roh_eingang.id as subcat_id). I hope this makes it a bit clearer? Link to comment https://forums.phpfreaks.com/topic/63705-count/#findComment-320371 Share on other sites More sharing options...
teng84 Posted August 10, 2007 Share Posted August 10, 2007 use a group by cause something like select count (id) from table group by skills Link to comment https://forums.phpfreaks.com/topic/63705-count/#findComment-320759 Share on other sites More sharing options...
mikenl Posted August 11, 2007 Author Share Posted August 11, 2007 Hi, Thx for the feedback. I have tried it but this influences the total number of rows found. If I do a GROUP BY, the total number of rows will be limited to that clause. I need to find the number of categories, subcategories and total number of rows in one query. Link to comment https://forums.phpfreaks.com/topic/63705-count/#findComment-320959 Share on other sites More sharing options...
fenway Posted August 20, 2007 Share Posted August 20, 2007 Assuming that you can retrieve all of these with a single query, WITH ROLLUP with help with your counts. Link to comment https://forums.phpfreaks.com/topic/63705-count/#findComment-329182 Share on other sites More sharing options...
mikenl Posted August 20, 2007 Author Share Posted August 20, 2007 I will study this, thx. I saw the topic was moved to javascript, which I don't think is correct, because it's a pure MySQL problem. Meanwhile I have split the query in 2. I first get the categories, and use IN to search in the array that is the first query. Link to comment https://forums.phpfreaks.com/topic/63705-count/#findComment-329246 Share on other sites More sharing options...
fenway Posted August 20, 2007 Share Posted August 20, 2007 I will study this, thanks. I saw the topic was moved to javascript, which I don't think is correct, because it's a pure MySQL problem. Huh? Link to comment https://forums.phpfreaks.com/topic/63705-count/#findComment-329322 Share on other sites More sharing options...
mikenl Posted August 21, 2007 Author Share Posted August 21, 2007 The problem is not with javascript. I only need to get the counts from mysql in order to use them in javascript, so the problem is with mysql. I just wanted to do all in one query, so that I don't have to "bother" the db twice for every page load. Link to comment https://forums.phpfreaks.com/topic/63705-count/#findComment-329555 Share on other sites More sharing options...
Illusion Posted August 21, 2007 Share Posted August 21, 2007 try this SELECT count(prostorage_roh_eingang.id) as cnt_subcat_id,count(prostorage_charge.charge) as cnt_cat_id FROM prostorage_charge,prostorage_roh_eingang,prostorage_roh where prostorage_roh_eingang.charge = prostorage_charge.charge AND prostorage_roh_eingang.id = prostorage_roh.id AND prostorage_charge.date < '$today' ORDER BY prostorage_roh_eingang.charge; I didn't check what I did Link to comment https://forums.phpfreaks.com/topic/63705-count/#findComment-329586 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.