I have a table with an ip address column. I would like to find the top five addresses which are listed.
Right now I'm planning it out the following:
Select all distinct ip addresses
Loop through them all saying count(id) where IP='{ip}' and storing the count
List the top five counts.
Downsides include what if I have 500 ip addresses. That's 500 queries I have to run to figure out what are the top five.
I'd like to build a query like so
select ip from table where 1 order by count({distinct ip}) asc limit 5
select ip, count(*)
from table
group by ip
order by count(*) desc limit 5
select IP, count(IP) as IPCount from TABLE
group by IP
order by IPCount DESC
Limit 5
There is :)
Related
The following query works great to count and get the total sum of visitors to my page.
SELECT id, COUNT(id) AS count
FROM visits
WHERE status <> 'test'
GROUP BY id
UNION ALL
SELECT 'SUM' id, COUNT(id)
FROM visits
WHERE status <> 'test'
But inside the visits table I have a column called IP where I store the IP of the visitors. I want to change the above query so that visits with the same IP are not counted (I only want to count unique IPs). How can I do it?
I have tried HAVING COUNT(IP) > 1 but it doesn't work.
Thanks!!!
I have a database structure of:
ip | yid | date
Where ip represents a user's IP and yid is a specific page ID. I'm trying to group by the ip and yid column, which I have done below. But then I need to group those results by the yid. So it would be grouping it into:
yid | count
Where count is the number of times that the page was called limiting 1 call per ip address.
Right now I have:
SELECT `ip`, `yid`, `time`, MAX(`time`), count(*)
FROM mp_downloads
GROUP BY CONCAT(`ip`, `yid`), `yid`
ORDER BY count(*) DESC
But it's not grouping the page ids after it groups by the distinct yid & ip combo.
Try This using nested query:
select temp.*, count(*) from
(SELECT `ip`, `yid`, `time`, MAX(`time`)
FROM mp_downloads
GROUP BY CONCAT(`ip`, `yid`))temp group by temp.yid;
The normative pattern for getting a count of ip addresses that have accessed a (yid) would be:
SELECT d.yid
, COUNT(DISTINCT d.ip) AS ip_count
, MAX(d.date) AS latest_time
FROM mp_downloads d
GROUP BY d.yid
For best performance, you'd want a suitable index, e.g.
... ON mp_downloads (yid, ip, date)
There's no need to group by an expression that concatenates two columns. There's no need for a derived table (inline view) or a subquery. If you don't need the latest_time, you can omit that expression.
I am using this code to count and sort from my database:
$qry = "select entertainer, count(*) from final_results group by entertainer order by count(*) desc";
I get the right result, in as much as it lists all the contents in order or popularity.
What I would like is the top 5 results to display, with a count for each of them and then a total count of all the results.
e.g.
Top response (10)
Second response (8)
Third response (6)
etc...
Total count = 56
I would appreciate any help and advice.
Thanks,
John C
You can get the totaly using WITH ROLLUP, but that won't work well with the LIMIT 5 you want in order to only fetch the top five results. (Edit:) It will also not work with the ordering, as discussed in the comments.
So you'll either have to fetch all results, not just the top 5, and sort them in the application, or use two distinct queries, possibly merged on the server side using UNION the way #RedFilter suggests. But if you do two separate queries, the I personally would rather issue each one separately from the client application, as splitting the total from the top five later on is too much work for little gain.
To fetch all results, you'd use
select entertainer, count(*)
from final_results
group by entertainer with rollup
To do two distinct fetches you'd use
select entertainer, count(*)
from final_results
group by entertainer
order by count(*) desc
limit 5
and
select count(*)
from final_results
If you want both in a single union, you can do this as
(select 1 as unionPart, entertainer, count(*) as count
from final_results
group by entertainer
order by count(*) desc
limit 5)
union all
(select 2 as unionPart, 'Total count', count(*) as count
from final_results)
order by unionPart asc, count desc
select entertainer, count
from (
(select entertainer, count(*) as count, 1 as sort
from final_results
group by entertainer
order by count(*) desc
limit 5)
union all
(select 'Total count', (select count(*) from final_results), 2 as sort)
) a
order by sort, count desc
I have a table
id ip
1 127.0.0.1
2 127.0.0.1
3 127.0.0.1
4 192.168.2.2
5 192.168.70.1
6 217.11.24.65
I need to get rows from 3 ips i.e. if i have limit 3 I must to get 1,2,3,4,5
If Limit 2 - 1,2,3,4
i.e. limited by unique IPs
SELECT id FROM ips LIMIT 3 // returns 1,2,3 but i want 1,2,3,4,5
Sorry for bad english and thanks for understanding.
SELECT id
FROM ips
WHERE ip IN (SELECT DISTINCT ip FROM ips ORDER BY id LIMIT 3)
As MySQL 5.5 doesn't yet support LIMIT inside of ANY, here's a simple workaround:
SELECT id
FROM ips
WHERE ip IN (SELECT * FROM (SELECT DISTINCT ip FROM ips ORDER BY id LIMIT 3) alias)
I have a column that takes user names. How can I count the number of instances of a users name. For example I have 10 rows and in column username i want to count all names that show up multiple times. I would like to build a list of the top contributors to my database. So if username alex shows up 5 times and jeff shows up 3 and april shows up 2 times i will count this and from that I can build my list.
Try GROUP BY:
SELECT username, COUNT(*) AS user_count
FROM yourtable
GROUP BY username
ORDER BY user_count DESC
Try something like
SELECT USER_NAME, COUNT(USER_NAME) FROM YOUR_TABLE GROUP BY USER_NAME;
If you want to get a count of all the usernames then you just do the following SQL:
Select Count(*) from tablename
If you want to get just the count of unique usernames
Select Count(*) from tablename Group by username