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

  • Home
  • SEARCH
  • 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 7024789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:53:17+00:00 2026-05-27T23:53:17+00:00

Given my following table structure: I’m trying to build a query that will return

  • 0

Given my following table structure:

enter image description here

I’m trying to build a query that will return the number of failed login attempts per IP since the last successful login of that same IP within the past hour.

For instance, you’ll notice there are several failed attempts (0) from a single IP within the past hour, but since the last successful login (#145) there has only been 1 (#146), which is what I want returned.

This query should also be dynamic and return rows of grouped IPs.

So far this is what I have, but I think @ipa is returning NULL

SELECT COUNT(*) tries, @ipa := login_ip
FROM login_log
WHERE login_id > (
    SELECT MAX(login_id)
    FROM login_log
    WHERE login_success = 1
    AND login_ip = @ipa
)
AND login_success = 0
AND login_date > NOW() - 3600
GROUP BY login_ip
ORDER BY tries DESC;

Thanks

  • 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-27T23:53:18+00:00Added an answer on May 27, 2026 at 11:53 pm

    Alias the table and try it this way, as your @ipa variable is doing nothing for you:

    SELECT COUNT(1) tries, l.login_ip
    FROM login_log l
    WHERE login_id > (
        SELECT MAX(login_id)
        FROM login_log l2
        WHERE l2.login_success = 1
        AND l2.login_ip = l.login_ip
    )
    AND login_success = 0
    AND login_date > NOW() - 3600
    GROUP BY login_ip
    ORDER BY tries DESC;
    

    Additionally, you can do this with a join:

    select
        count(1) tries,
        log1.login_ip
    from
        login_log log1
        inner join (
            select
                login_ip,
                max(login_date) as max_date
            from
                login_log
            where
                login_success = 1
            group by
                login_ip
        ) log2 on
            log1.login_ip = log2.login_ip
    where
        log1.login_success = 0
        and log1.login_date > NOW() - 3600
        and log1.login_date > log2.max_date
    group by
        login_ip
    order by tries desc
    

    You can try both ways and see which one is faster for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following table structure, how can I use a Linq query to return
I have a necessity to sort a given HTML table of the following structure,
Given the following table structure: CREATE TABLE user ( uid INT(11) auto_increment, name VARCHAR(200),
Given the following table structure: CREATE TABLE foo ( ID INT NOT NULL AUTO_INCREMENT,
I have a organization name table with the following structure given below: CREATE TABLE
given the following sample table structure is there a way to add to a
Given the following table structure: CREATE TABLE IF NOT EXISTS `roles` ( `id` int(11)
Given the following table structure: id iata icao name 1 ANC PANC Anchorage -
Given the following simple table structure (SQL Server 2008), I want to be able
Given the following table structure: CrimeID | No_Of_Crimes | CrimeDate | Violence | Robbery

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.