dreamwest Posted June 30, 2009 Share Posted June 30, 2009 Im trying to make a query to tell how many videos each user has uploaded, but the there are two tables and i need to sort by amount of videos descending Heres what ive done $query = "SELECT videos.*, signups.* ". "FROM videos, signups ". "WHERE videos.user_id = signups.user_id limit 5 "; $result = mysql_query($query) or die(mysql_error()); // Print out the contents of each row into a table while($row = mysql_fetch_array($result)){ echo $row['username']. " has ". $row[' ']." videos"; echo "<br />"; } Heres my table structure Table1: signups (tabe name) user_id | username Table 2: videos (Table name) id | user_id I proberly need a count or group in here but im uncertain Quote Link to comment https://forums.phpfreaks.com/topic/164226-select-group-order-by-join-all-in-one-query/ Share on other sites More sharing options...
dzelenika Posted June 30, 2009 Share Posted June 30, 2009 SELECT sugnups.username, Count(*) FROM videos, signups WHERE videos.user_id = signups.user_id GROUP BY sugnups.username ORDER BY 2 DESC Quote Link to comment https://forums.phpfreaks.com/topic/164226-select-group-order-by-join-all-in-one-query/#findComment-866260 Share on other sites More sharing options...
dreamwest Posted June 30, 2009 Author Share Posted June 30, 2009 Thanks. The query works, but im still having trouble with the while loop while($row = mysql_fetch_array($result)){ echo $row['username']." has ".$row['COUNT(id)']." videos"; echo "<br />"; } The username is grouped and echos but doesnt have any video count Quote Link to comment https://forums.phpfreaks.com/topic/164226-select-group-order-by-join-all-in-one-query/#findComment-866765 Share on other sites More sharing options...
dzelenika Posted July 1, 2009 Share Posted July 1, 2009 Thanks. The query works, but im still having trouble with the while loop while($row = mysql_fetch_array($result)){ echo $row['username']." has ".$row['COUNT(id)']." videos"; echo "<br />"; } The username is grouped and echos but doesnt have any video count Put $row[1] Instead of $row['COUNT(id)'] Quote Link to comment https://forums.phpfreaks.com/topic/164226-select-group-order-by-join-all-in-one-query/#findComment-867012 Share on other sites More sharing options...
fenway Posted July 2, 2009 Share Posted July 2, 2009 Oh, please don't do that... don't rely on array offsets. Use a column alias and refer to it that way. Quote Link to comment https://forums.phpfreaks.com/topic/164226-select-group-order-by-join-all-in-one-query/#findComment-867488 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.