Database query involving username and count
I have a table called tbl_users
A broken down version of it for this task
id | username | ref
I am trying to count the ref column for most entries and order by most to least.
This column displays users. So if there are 10 r开发者_StackOverflowows with "User 1" in the ref column it would return User 1 10.
How can I put this together?
EDIT. I have put together the following query (using the 5 answers, thanks).
Is throwing up an error, saying the query isn't valid basically.
$q = "SELECT ref, count(ref) as total_count FROM ".TBL_USERS." WHERE ref != 'NONE' GROUP BY ref ORDER BY total_count DESC";
Any ideas why?
ALL FIXED!!
edit: Hmm. You have 'user 1' in the ref column, and want to count that. check.
Something like this should work?
SELECT ref, COUNT(ref) FROM tbl_users
GROUP BY ref
ORDER BY COUNT(ref) DESC
follow the link provided below
http://forums.mysql.com/read.php?97,26993,26993
TRY
SELECT username, COUNT(ref)
FROM tbl_users
GROUP BY id
ORDER BY COUNT(ref) ASC
SELECT *, COUNT(ref) AS ref_count FROM tbl_users
GROUP BY ref
ORDER by ref_count DESC
精彩评论