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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:55:32+00:00 2026-06-12T01:55:32+00:00

This is my query: Select COUNT(*) From Users u Inner Join UsersLoginHistory uh On

  • 0

This is my query:

Select COUNT(*) 
From 
    Users u
    Inner Join 
    UsersLoginHistory uh On u.UserID = uh.UserID 
Where 
    1 = 1   
    And 
    u.AccountID = 37 
Group By u.UserID

What I’d like to be able to get is Count(*) should be returning a number after grouping on u.UserId. But it returns the Count(*) before the group by is made.

So I can rewrite the above query as:

Select COUNT(*)
From (
    Select   u.Username   
    From 
        Users u
        Inner Join 
        UsersLoginHistory uh On u.UserID = uh.UserID 
    Where 
        1 = 1   
        And 
        u.AccountID = 37 
    Group By u.UserID
) v

But I need to find out why is the Count(*) returning records before a group by is made and how can I fix the 1st query itself.

EDIT: Sample Records

Users table

UserId     Username
102        tom.kaufmann

UserLoginHistory table

UsersLoginHistoryID UserID  LoginDateTime         LogoutDateTime         IPAddress
1                    102    2012-09-28 01:16:00 NULL                 115.118.71.248
2                    102    2012-09-28 01:29:00 2012-09-28 01:29:00  127.0.0.1
3                    102    2012-09-28 01:32:00 2012-09-28 01:32:00  127.0.0.1
4                    102    2012-09-28 01:41:00 NULL                 115.118.71.248
5                    102    2012-09-28 01:43:00 2012-09-28 07:04:00  115.118.71.248  

and so on..

Haven’t writted every single record in this DB.

  • 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-06-12T01:55:33+00:00Added an answer on June 12, 2026 at 1:55 am

    Based on your second query which you say returns the desired results (and assuming UserID is the PK of Users) I presume this is what you need

    SELECT Count(UserID)
    FROM   Users u
    WHERE  u.AccountID = 37
           AND EXISTS (SELECT *
                       FROM   UsersLoginHistory uh
                       WHERE  u.UserID = uh.UserID)
    

    This will be more efficient than expanding out all the joined rows then collapsing them again with Group By u.UserID and counting the number of rows that result.

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

Sidebar

Related Questions

I have a very simply query like this: SELECT users.id, COUNT(others.id) as c FROM
I have an sql query like this: SELECT IF( (SELECT COUNT(*) FROM `test_table2` WHERE
I have a mysql query like this: $topicreplycount = mysql_query('SELECT COUNT(DISTINCT post_msg_id) FROM phpbb_attachments
I have a query like this: SELECT COUNT(*) AS amount FROM daily_individual_tracking WHERE sales
MY query looks like this: SELECT COUNT(entryID) FROM table WHERE date >= DATE_SUB(CURDATE(), INTERVAL
I have 2 tables, using an inner join to query them. SELECT COUNT(table2.id) FROM
SET @v1 := SELECT COUNT(*) FROM user_rating; SELECT @v1 When I execute this query
I have this query: SELECT COUNT(articles.id) AS count FROM articles, xml_documents, streams WHERE articles.xml_document_id
How do I formulate this query update forge..dimInteg2 set duplicates = (select count(*) from
i have this simple query : SELECT YEAR(P.DateCreated) ,MONTH(P.DateCreated) ,COUNT(*) AS cnt FROM tbl1,

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.