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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:43:50+00:00 2026-06-09T05:43:50+00:00

I need to craft a query in mysql that will return 12 rows (one

  • 0

I need to craft a query in mysql that will return 12 rows (one row for each month) that selects the month name, and the count of records for the given month. I have two tables, a months_tbl and events_tbl. Each record in the events_tbl has a datetime column and a company_id column.

I’m currently doing something like (notice I don’t have the WHERE company_id clause yet):

SELECT months_tbl.month, COUNT( events_tbl.event_id ) cnt
FROM months_tbl
LEFT JOIN events_tbl ON months_tbl.month_id = MONTH( events_tbl.appt_date_time )
GROUP BY months_tbl.month
ORDER BY months_tbl.month_id ASC;

This returns something like what I’m expecting (12 rows selected, with a count of events for the month, or zero if there were none):

**month**    **cnt**
January      0
February     0
March        0
April        0
May          0
June         0
July         0
August       88
September    99
October      120
November     0
December     9

however it’s returning all records regardless of company. I need to make sure the query is filtered by so, I added the where clause:

SELECT months_tbl.month, COUNT( events_tbl.appt_id ) cnt
FROM months_tbl
LEFT JOIN events_tbl ON months_tbl.month_id = MONTH( events_tbl.appt_date_time ) 
WHERE events_tbl.company_id = 1 
GROUP BY months_tbl.month
ORDER BY months_tbl.month_id ASC;

When I add the where clause my results become:

**month**    **cnt**
August       88
September    99
October      120
December     9

Any ideas why I’m losing all the other months records when I add the where clause, even though I’m using a left join?

  • 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-09T05:43:51+00:00Added an answer on June 9, 2026 at 5:43 am

    You are using a LEFT JOIN, but in your where statement your making it a ‘normal’ JOIN

    Try writing it like this:

    SELECT months_tbl.month, COUNT( events_tbl.appt_id ) cnt
    FROM months_tbl
      LEFT JOIN events_tbl ON (months_tbl.month_id = MONTH(events_tbl.appt_date_time) 
        AND events_tbl.company_id = 1
      )
    GROUP BY months_tbl.month
    ORDER BY months_tbl.month_id ASC;
    

    Note the AND events_tbl.company_id = 1 is in the LEFT JOIN

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

Sidebar

Related Questions

I need to craft a complex regex expression that will match a varying number
Need help with a query that I wrote: I have three tables Company id
I need to do some reports that aren't strictly a listing-of-entities. More free-form query,
I need to craft a packet that has a header, a trailer, and a
Need a way for one service on a well-known Endpoint to return strings which
I need to craft a filter for an object that checks date ranges. Right
A MySQL query retrieves: totalpoints -- name 55 -- John Doe 55 -- John
Need to host some formatted messages in div elements, so that the formatting will
I have a script that connects to a mine craft server, receives packets, and
I have inherited a really bad access database that I need to move the

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.