php - Order results by count with MySQL -
i have table stores list of tags. able write script displays tags user uses most. but, having trouble writing mysql query so.
table structure:
tag_id | user_id | tag -------------------------------- 1 | 1 | hiking 2 | 1 | fishing 3 | 1 | hiking 4 | 1 | swimming 5 | 1 | hiking 6 | 1 | swimming
what have far outputs of tags:
$query= "select tag tags user_id='1'";
i results this:
hiking swimming fishing
can use order "count" or achieve this?
you need order count
try this:
select tag tags group tag order count(tag) desc
output:
╔══════════╗ ║ tag ║ ╠══════════╣ ║ hiking ║ ║ swimming ║ ║ fishing ║ ╚══════════╝
see sqlfiddle
update:
you can show counts this:
select tag ,count(tag) `count` tags group tag order count(tag) desc
Comments
Post a Comment