Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 252003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:38:31+00:00 2026-05-11T21:38:31+00:00

I have a table containing pagehit (normalized) data and I need to grab the

  • 0

I have a table containing pagehit (normalized) data and I need to grab the 10 latest unique ips.

I tried to do it like this:

SELECT * FROM spy_hits ORDER BY date desc GROUP BY ip LIMIT 10;

Which should give me this result:

+-----+------------+-----+---------+----+------+------+---------+-------+-------+
| id  | date       | ip  | browser | os | page | host | referer | query | agent |
+-----+------------+-----+---------+----+------+------+---------+-------+-------+
| 354 | 1244442065 |   2 |       3 |  2 |   16 |    1 |      47 |    12 |     2 |
| 311 | 1244442000 |   1 |       2 |  1 |   16 |    1 |      36 |    12 |     1 |
+-----+------------+-----+---------+----+------+------+---------+-------+-------+
2 rows in set (0.00 sec)

That is the latest unique visitors to the site.

But I get a syntax error instead of that result.

So I have to do this query:

SELECT * FROM spy_hits GROUP BY ip ORDER BY date desc LIMIT 10;

Which I thought would be ok. But it gives this result:

+-----+------------+-----+---------+----+------+------+---------+-------+-------+
| id  | date       | ip  | browser | os | page | host | referer | query | agent |
+-----+------------+-----+---------+----+------+------+---------+-------+-------+
| 280 | 1242130841 |   2 |       3 |  2 |   16 |    1 |      47 |    12 |     2 |
| 268 | 1242130818 |   1 |       2 |  1 |   16 |    1 |      36 |    12 |     1 |
+-----+------------+-----+---------+----+------+------+---------+-------+-------+
2 rows in set (0.00 sec)

But that sets order by date after its grouped already so it grabs the first unique ips in the table which are also the oldest.

So the table goes:

 id --- date
 268    1242130818    (Old)
  |      |
  V      V
 354    1244442065    (New)

But I want it to go like this before I do the group by:

 id --- date
 354    1244442065    (New)
  ^       ^
  |       |
 268    1242130818    (Old)

I’m using PHP with it so if anyone has an idea of how to get the results with a PHP solution.

Cheers in advance 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:38:31+00:00Added an answer on May 11, 2026 at 9:38 pm

    If you have few DISTINCT IP‘s:

    SELECT  ip, MAX(date) AS maxdate
    FROM    (
            SELECT  ip, MAX(date) AS maxdate
            FROM    spy_hits
            GROUP BY
                    ip
            )
    ORDER BY
            maxdate DESC
    LIMIT 10
    

    If you have lots of DISTINCT IP‘s:

    SELECT  *
    FROM    spy_hits so
    WHERE   NOT EXISTS
            (
            SELECT  1
            FROM    spy_hits si
            WHERE   si.ip = so.ip
                    AND si.date > so.date
            )
    ORDER BY
            date DESC
    LIMIT 10
    

    Creating two indexes on this table:

    CREATE INDEX ix_spyhits_date ON spy_hits (date)
    CREATE INDEX ix_spyhits_ip_date ON spy_hits (ip, date)
    

    will improve these queries a lot.

    See this entry in my blog for performance details:

    • Latest DISTINCT records
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 133k
  • Answers 133k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The $_GET variable provides an array of all the variables… May 12, 2026 at 6:34 am
  • Editorial Team
    Editorial Team added an answer Check Wizardby: it provides a special language (somewhat close to… May 12, 2026 at 6:34 am
  • Editorial Team
    Editorial Team added an answer Every request is potentially using a different port. Since most… May 12, 2026 at 6:34 am

Related Questions

I have a table containing prices for a lot of different things in a
I have a table containing the runtimes for generators on different sites, and I
I have a table containing hundreds of entries and I am trying to delete
I have a table containing cells with text of various lengths. It is essential

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.