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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:20:27+00:00 2026-06-18T02:20:27+00:00

I have query I need to perform in MySQL on three linked tables. I

  • 0

I have query I need to perform in MySQL on three linked tables. I can do it the lazy way with nested queries but I cannot work out how to do it with a single query.

Tables are:

Area:-
 : id     (int)
 : name   (string)

Consultant:-
 :id      (int)
 :active  (1/0)

ConsArea:-
 : areaID        (int)
 : consultantID  (int)

I need to loop through all the areas (with $area variable) so that I list ALL areas and against each area to indicate the number of ‘active’ consultants … so all areas must be listed with a value next to it (which can be zero if there are no active consultants associated)

The first part of the query (irrespective of consultant active or not) I can do with:

      SELECT areas.name AS aname, COUNT(consAreas.areaID) AS cct 
        FROM areas LEFT OUTER JOIN consAreas 
          ON consAreas.areaID = areas.id 
       WHERE areas.areaID = $area 
    GROUP BY areas.id 
    ORDER BY areas.name

.. but when I want to include the condition of the consultant being active I cannot work out the correct join. It only lists the areas with > 0 active consultants whereas I need all areas.

      SELECT areas.name AS aname, COUNT(consAreas.area) AS cct 
        FROM areas LEFT OUTER JOIN consAreas 
          ON consAreas.area = areas.id 
        **JOIN consultants ON consultants.id = consAreas.cons**
       WHERE areas.areaID = $area 
         **AND consultants.active = 1**
    GROUP BY areas.id 
    ORDER BY areas.name

Anyone 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-18T02:20:28+00:00Added an answer on June 18, 2026 at 2:20 am

    This is because of a behaviour of mysql. An inner join following a left join makes that left join an inner join.

    SELECT areas.name AS aname, COUNT(consultants.id) AS cct 
    FROM areas
        LEFT JOIN consAreas ON consAreas.area = areas.id 
        LEFT JOIN consultants ON consultants.id = consAreas.cons AND consultants.active = 1
    WHERE 
        areas.areaID = $area 
    GROUP BY areas.id 
    ORDER BY areas.name
    

    Here you can see I’m only using left joins, and more importantly filtering the consultants.active status from the left join ON clause directly.

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

Sidebar

Related Questions

I need to have a query string in my crontab but it isn't working.
I need to have MySQL query like this one: UPDATE table_name SET 1 =
I have a query that does what i want joining table but i need
I have a table I need to perform a horrible query on (not my
Overview: I have three tables 1) subscribers, bios, and shirtsizes and i need to
I have a question about the fastest way to perform a SQL Server query
I need to perform a query in MySQL that returns distinct values for product_id
I have a query that I need to execute that I do not know
I have a broker query where I need to sort by 2 different fields
I have a query such that I need to get A specific dog All

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.