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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:23:40+00:00 2026-06-16T02:23:40+00:00

Following are my two queries in which by adding only one more left join

  • 0

Following are my two queries in which by adding only one more left join result gets messed up. 2nd query result is accurate but in 1st query by adding one more left join output is not right (as you can see i only added one more left join but i didnot put any filteration for that join in where clause) . Kindly let me know how can i fix this? Thanks,

1st Query:

SELECT Distinct  count(event_id) as event_count from events 
Left Join events on events.event_id = my_events.i_event_id 
Left Join atdees on events.event_id = atdees.fk_event_id

where my_events.v_title != "NULL" and  r_present = 1 and resident_id = '208'  and event_atd > date_sub(curdate(), interval 37 day) group by event_count  order by event_count desc limit 5

Result:

26  |  12   |  11   |   10

2nd Query:

SELECT Distinct  count(event_id) as event_count from events 
Left Join events on events.event_id = my_events.i_event_id 

where my_events.v_title != "NULL" and  r_present = 1 and resident_id = '208'  and event_atd > date_sub(curdate(), interval 37 day) group by event_count  order by event_count desc limit 5

Result:

2  |  1   |  1   |   1
  • 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-16T02:23:41+00:00Added an answer on June 16, 2026 at 2:23 am

    What the left join does is that it adds extra (duplicate) rows.

    If you strip out the count and just list the rows, you’ll see many duplicate rows.
    This is because you’re asking for the cross product between events and atdees; and of course there are many more combinations of events and atdees then there are just events.

    Basic logic really.

    Change your top query to

    SELECT count(distinct event_id) as event_count from events 
    Left Join events on events.event_id = my_events.i_event_id 
    Left Join atdees on events.event_id = atdees.fk_event_id
    
    where my_events.v_title != "NULL" and  r_present = 1 and resident_id = '208'
    and event_atd > date_sub(curdate(), interval 37 day) 
    group by event_count  
    order by event_count desc 
    limit 5
    

    And you should be getting the same results (although this is much slower)

    Further note that left joins do not filter, they add stuff.
    If you want to filter you’d use inner joins
    (This is a gross oversimplification)

    About distinct
    The distinct keyword can work inside and outside of an aggregate function.
    If you use it inside, it will only count (sum etc) unique values.
    If you use it outside the aggregate function it will only list unique rows.

    That is: distinct on its own eliminates duplicate rows (it works on all columns in the result set).

    distinct in a function works as a filter for that function only.

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

Sidebar

Related Questions

I have the following two queries which I want to get in one query.
Following are two queries. which one is good as per query optimization point of
I have the following two queries. SELECT account_name,SUM(amount) AS AMOUNT1 FROM entries LEFT JOIN
I have the following query, which fetches data from only one table. EDIT: It
Which one is faster of the following two queries? 1 SELECT TOP 1 order_date
I have the following two queries (*) , which only differ in the field
Given the following two queries, which one would be faster (in theory) if there
I have the following two queries, I believe that the one that uses the
Which of the two following queries might be faster? $cposts = mysql_query(SELECT id FROM
I have the following two YQL queries, each of which work fine on their

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.