Reputation: 138
i will take some information from my database.
My Database: (log)
______________
| id | browser |
______________
Now I have about 20 log entries, and want to know the 3 most registered browser. The browsers are "Firefox, Chrome, Safari, IE". The problem is, I do not like the Mysql query should look like. I've always just get a query to one browser out which looked like this.:
$firefox_query = mysqli_query($sql, "SELECT COUNT(id) AS anzahl FROM log WHERE browser LIKE 'Firefox'");
Sorry for my bad english.
Upvotes: 0
Views: 116
Reputation: 13474
SELECT browser,
COUNT(*) AS browse
FROM tablename
GROUP BY browser
ORDER BY id DESC LIMIT 3
Upvotes: 1
Reputation: 1136
$most_registered = mysqli_query($sql, "SELECT browser, COUNT(id) AS anzahl FROM log GROUP BY browser ORDER BY anzahl DESC LIMIT 3");
Upvotes: -1
Reputation: 95
you need something like: select count(browser),browser from anzahl where browser in( 'FireFox','Safari','IE') group by browser order by 1 desc;
note that in your example like is misused. Like is to find a pattern, when your text is equal to something here 'FireFox' , then you use: WHERE browser ='Firefox'
Upvotes: 1
Reputation: 2314
Try this:
SELECT
COUNT(*) AS count,
browser
FROM
log
GROUP BY
browser
ORDER BY
count DESC
LIMIT 3
Upvotes: 1
Reputation: 44844
This is what you can do
select
count(*) as tot,
browser
from log
group by browser
order by tot desc
limit 3
Upvotes: 1