Isis
Isis

Reputation: 4666

MySQL Select and LIMIT

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.

Upvotes: 0

Views: 1585

Answers (1)

Dmytro Shevchenko
Dmytro Shevchenko

Reputation: 34581

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)

Upvotes: 4

Related Questions