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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:23:01+00:00 2026-06-04T07:23:01+00:00

I have these tables: users —– id INT name VARCHAR(20) email VARCHAR(40) user_fans ———-

  • 0

I have these tables:

users
-----
id                  INT
name                VARCHAR(20)
email               VARCHAR(40)

user_fans
----------
id                  INT
user_id             INT  /* linked to users.id */
fan_id              INT  /* linked to users.id */
time_created        INT  /* unix timestamp */

I can get all rows from table users with additional field named num_fans using the following query

SELECT u.id, u.name, u.email, COUNT(f.id) AS num_fans
FROM users u
LEFT JOIN user_fans f ON u.id=f.user_id
GROUP BY u.id, u.name, u.email
ORDER BY num_fans DESC

The problem is I need to get the num_fans in a range of time. I tried this query

SELECT u.id, u.name, u.email, COUNT(f.id) AS num_fans
FROM users u
LEFT JOIN user_fans f ON u.id=f.user_id
WHERE f.time_created > UNIX_TIMESTAMP('2012-5-1 00:00:00')
GROUP BY u.id, u.name, u.email
ORDER BY num_fans DESC

But the query above will return only users which already have fans. I want the rest of users also returned with num_fans=0.

Thanks for your help.

  • 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-04T07:23:02+00:00Added an answer on June 4, 2026 at 7:23 am

    Move the condition to the on:

    SELECT u.id, 
           u.name, 
           u.email, 
           COUNT(f.id) AS num_fans 
    FROM users u LEFT JOIN user_fans f 
                 ON u.id=f.user_id 
                 and f.time_created > UNIX_TIMESTAMP('2012-5-1 00:00:00') 
    GROUP BY u.id, u.name, u.email ORDER BY num_fans DESC 
    

    Update
    (Added an explanation on @Gardon Dave’s suggestion)

    The difference between what you have and this is:

    In your query you have the condition in the WHERE clause. This is applied after the Join,(see this as filtering on top of the join). If a row does not follow this condition it will be eliminated. Outer joined rows that do not have an entry in the user_fans table will not satisfy this condition (value will be NULL).

    Having the condition in the on will join the users table only to the subset of the user_fans table – the outer join happens after this filtering.

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

Sidebar

Related Questions

I have these tables: USERS: user_ID username password email data_registered POSTS: post_ID user_ID post_title
Strange situation. If I have these tables: CREATE TABLE t1 (id INT, title VARCHAR(20),
I have these 3 tables: users table(PK user_id) Fields: user_id, user_first_name, user_last_name, username, user_email...etc
i have these two tables , s(id,firstname,lastname,mydate,mount,sh) users(uid,email,password,nameoffice) and this query $sql=SELECT s.id,s.firstname,s.lastname,s.mydate,s.mount,users.nameOffice FROM
I have these tables: CREATE TABLE User( Username varchar(15) NOT NULL, Name varchar(20) DEFAULT
I have these two database tables: locations id name users id location_id last_name first_name
I have these two tables tickets - id, user_id, manager_id etc users - id,
Suppose you have these tables: Table Name: Salesman Fields: S_ID(Primary Key), Name Table Name:
I have to tables:users and messages.And create the one-many relations between these tables.And,If I
I have these 2 tables categories with fields id, category, total users with fields

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.