phpknight Posted December 28, 2007 Share Posted December 28, 2007 Hi, I know that I can use COUNT(*) to get the number of rows in a table, etc. However, is there a way I can run a relatively complex query and just get the number of rows returned to me instead of all the data? For instance, I want to just count how many images are in a gallery of a certain user--to get this I have to do a complex join with multiple tables, but if they have thousands, I just want the number. I really do not need thousands of rows. Does anybody know how to do this? I seem to be having trouble accomplishing this. Quote Link to comment https://forums.phpfreaks.com/topic/83517-solved-getting-number-of-rows-in-a-complex-query/ Share on other sites More sharing options...
Barand Posted December 28, 2007 Share Posted December 28, 2007 The structure of the multiple tables would help us to help you Quote Link to comment https://forums.phpfreaks.com/topic/83517-solved-getting-number-of-rows-in-a-complex-query/#findComment-424955 Share on other sites More sharing options...
drranch Posted December 29, 2007 Share Posted December 29, 2007 example structure table name: image Column name: userid Column name: image $query = "SELECT userid, COUNT(*) FROM image GROUP BY userid Quote Link to comment https://forums.phpfreaks.com/topic/83517-solved-getting-number-of-rows-in-a-complex-query/#findComment-425110 Share on other sites More sharing options...
phpknight Posted December 29, 2007 Author Share Posted December 29, 2007 Thanks for the input. I was able to fix this by rewriting the query. It is much more efficient now. But, basically the old question is something like this: In a query when you have SELECT DISTINCT, how do you get the count of the number of distinct rows here. If I put count to the left, that is bad syntax. However, if I take distinct out, then the results are not what I want. Quote Link to comment https://forums.phpfreaks.com/topic/83517-solved-getting-number-of-rows-in-a-complex-query/#findComment-425270 Share on other sites More sharing options...
Barand Posted December 29, 2007 Share Posted December 29, 2007 COUNT(DISTINCT imageID) as numImages may help Quote Link to comment https://forums.phpfreaks.com/topic/83517-solved-getting-number-of-rows-in-a-complex-query/#findComment-425296 Share on other sites More sharing options...
phpknight Posted December 29, 2007 Author Share Posted December 29, 2007 Aha, thanks for the tip! Quote Link to comment https://forums.phpfreaks.com/topic/83517-solved-getting-number-of-rows-in-a-complex-query/#findComment-425304 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.