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 631819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:00:32+00:00 2026-05-13T20:00:32+00:00

My query needs to do this get user details get last login out of

  • 0

My query needs to do this

  • get user details
  • get last login out of a table of logins that are logged
  • get number of failed logins, but only from the last 3 logins of that user

My problem seems to be in the subquery. I want the WHERE part of that to reference the user_id that was matched earlier in the outer query. Is that even possible? Have I made a huge mess of this query?

$query = 'SELECT users.id, 
                         users.email, 
                         users.password, 
                         users.first_name, 
                         users.last_name,
                         max(user_logins.datetime) as last_login,
                         count(user_logins.failed) as failed_logins,

            FROM users, user_logins
            WHERE users.email = ' . Database::instance()->escape($email) . '
                AND users.id = user_logins.user_id
                AND user_logins.datetime IN (SELECT datetime FROM user_logins WHERE user_id = users.id  )
            LIMIT 1';

The last_login seems to be working great. The reason I want the last 3 is so that I can check if the last 3 are failed, then set a timeout for this user to log in.

I can not go back and do an UPDATE users SET failed = 1 WHERE failed = 0 because then I won’t have an accurate log of user logins. I’d like to see any that fail.

What am I doing wrong?
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-13T20:00:32+00:00Added an answer on May 13, 2026 at 8:00 pm

    I would try the following. It should give you the three latest logins in a sub-query, SUM up the number of failed logins and get the time of the last login.

    MySQL allows not to list every column selected in the GROUP BY, with other databases you would have to add all columns of users that you need to the GROUP BY.

    $query = '
    SELECT u.id, 
           u.email, 
           u.password, 
           u.first_name, 
           u.last_name,
           MAX( ul.datetime ) as last_login,
           SUM( ul.failed ) as failed_logins,
    FROM users u
    JOIN ( SELECT user_id, datetime, failed
           FROM user_logins
           ORDER BY datetime DESC
           LIMIT 3 ) ul
      ON ( u.id = ul.user_id )
    WHERE u.email = ' . Database::instance()->escape($email) . '
    GROUP BY u.id
    ';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a reasonably simple query (this time) that I need ALL the results
Here is the issue I am having: I have a large query that needs
I need to test a function that needs to query a page on an
I'm writing a query for an application that needs to list all the products
I need to shorten this query and while I'm pretty good at SQL, I'm
I am newbie to db programming and need help with optimizing this query: Given
So I have an SQL dump file that needs to be loaded using mysql_query().
I need to query a table for values given a string. The table is
I have a function that returns a table. The returned table contains (among other
This is my setup: Table files: id (PK), filename, user_id, date, filesize Table scores:

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.