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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:29:05+00:00 2026-05-31T18:29:05+00:00

I have a very simple table: +———-+————–+——+—–+———+——-+ | Field | Type | Null |

  • 0

I have a very simple table:

+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| time     | int(11)      | YES  |     | 0       |       |
| username | varchar(120) | NO   | MUL |         |       |
| ip       | varchar(40)  | NO   | MUL |         |       |
| failed   | varchar(40)  | NO   | MUL |         |       |
+----------+--------------+------+-----+---------+-------+

I’m looking to query for a few specific users and I want both a count of logins and the most recent login (if any logins exist). I’ve tried various combinations of “GROUP BY” and nothing is quite working.

My simple count query is this:

mysql> SELECT count(*), username FROM logins WHERE username='foo@bar.com' and failed='0' group by username;

How can I still get a single row back that contains both a count and the last login?

Bonus points if it turns the timestamp into human-friendly format.

edit: The first answer seems the clearest to me, and that worked well.

I also came up with this, which seems to work and returns in about the same amount of time, but I’m not sure what I’m doing there with the nested SELECT:

SELECT COUNT(*), username (SELECT MAX(time)) FROM logins WHERE username='foo@bar.com' AND failed='0' GROUP BY username;

And to get a human-readable time:

SELECT COUNT(*), username (SELECT FROM_UNIXTIME(MAX(time))) FROM logins WHERE username='foo@bar.com' AND failed='0' GROUP BY username;

Both this version and the more proper looking query in the first answer take about 2m 27s (the table has about 161M rows).

  • 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-31T18:29:06+00:00Added an answer on May 31, 2026 at 6:29 pm

    Max(time) will give you the latest login time. See the full query bellow

    SELECT username, 
           COUNT(*)    AS `count`, 
           Max(`time`) AS `latest` 
    FROM   logins 
    WHERE  username = 'foo@bar.com' 
           AND failed = '0' 
    GROUP  BY username; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple set-up. Table Node has a nullable foreign key ObjectId.
I have a very simple table with only one column (+ the key column).
Given the following very simple table: Create Table tblUserLogins ( LoginNumber int PRIMARY KEY
I have very simple data model like this: create table Company ( id int
I have a very simple table with two columns, but has 4.5M rows. CREATE
I have the following PHP code doing a very simple select into a table.
I have a very simple webforms app that will allow field techs to order
I have a very simple table on my website, that displays different URL's. I
I'm trying to store XML in SQL 2005. I have a very simple table
I have a very simple HTML table with 4 columns: Facility Name, Phone #,

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.