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

  • Home
  • SEARCH
  • 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 6365529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:18:19+00:00 2026-05-25T00:18:19+00:00

(query updated for cdhowie comments) Here’s the situation. I want to count the number

  • 0

(query updated for cdhowie comments)

Here’s the situation.
I want to “count the number of tasks assigned to each worker within kind 1,2 of task location AND kind 3,4 of task department”.

Suppose I have the following tables

Task : id, Name
Task_Worker_Combi : Task_id, Worker_id
Worker : id, Name
Task_Location_Combi : Task_id, Location_id
Task_Department_Combi : Task_id, Department_id
Location : id, Name
Department : id, Name

I got as far as the following:(however since it takes forever there must be something wrong with the query)

SELECT W.id, W.Name, COUNT(TWC.Task_id) AS Count
FROM Worker AS W
LEFT JOIN Task_Worker_Combi AS TWC
    ON (W.id=TWC.Worker_id)
WHERE W.id>0 AND TWC.Task_id IN 
(
    SELECT T.id
    FROM Task as T
    LEFT JOIN (Task_Location_Combi AS TLC, Task_Department_Combi AS TDC) 
        ON (T.id=TLC.Task_id AND T.id=TDC.Task_id)
    WHERE 1 AND TLC.Location_id IN (1,2) AND TDC.Department_id IN (3,4)
    GROUP BY T.id
)
GROUP BY W.id
ORDER BY W.Name

Without this subquery it returns “the number of tasks assigned to each worker unconditionally” fine.

AND TWC.Task_id IN 
(
    SELECT T.id
    FROM Task as T
    LEFT JOIN (Task_Location_Combi AS TLC, Task_Department_Combi AS TDC) 
        ON (T.id=TLC.Task_id AND T.id=TDC.Task_id)
    WHERE 1 AND TLC.Location_id IN (1,2) AND TDC.Department_id IN (3,4)
    GROUP BY T.id
)

Where went wrong, and how would you rewrite this query to efficiently work? Please help me somebody. I’m stuck here for over a week now!

The actual query is the following. (assume Job as Task and Worker as Industry from above simplified query)

EXPLAIN SELECT id, Name, COUNT( J.Industry ) AS Count
FROM industry_db.industry AS I
LEFT JOIN job_db.industry AS J ON ( I.id = J.Industry ) 
WHERE I.id >0
AND J.Job
IN (

SELECT t1.id
FROM job_db.job AS t1
LEFT JOIN (
company_db.company AS t2, job_db.industry AS t3, location_db.city AS t4, job_db.function AS t5, job_db.tag AS t6, job_db.degree AS t7, location_db.state AS t8, location_db.group AS t9
) ON ( t1.Company = t2.id
AND t1.id = t3.Job
AND t1.City = t4.id
AND t1.id = t5.Job
AND t1.id = t6.Job
AND t1.id = t7.Job
AND t1.State = t8.id
AND t1.State_Group = t9.id ) 
WHERE 1 
AND t1.Open = '1'
GROUP BY t1.id)
GROUP BY id
HAVING Count >0
ORDER BY Name

And the Explain result from phpmyadmin is the following.

id  select_type  table  type  possible_keys  key  key_len  ref  rows  Extra  
1 PRIMARY I range PRIMARY,id PRIMARY 1 NULL 39 Using where; Using temporary; Using filesort 
1 PRIMARY J ref Industry Industry 1 industry_db.I.id 403 Using where 
2 DEPENDENT SUBQUERY t1 index NULL PRIMARY 4 NULL 2868 Using where 
2 DEPENDENT SUBQUERY t9 eq_ref PRIMARY PRIMARY 1 job_db.t1.State_Group 1 Using index 
2 DEPENDENT SUBQUERY t2 eq_ref PRIMARY PRIMARY 2 job_db.t1.Company 1 Using index 
2 DEPENDENT SUBQUERY t8 eq_ref PRIMARY PRIMARY 1 job_db.t1.State 1 Using index 
2 DEPENDENT SUBQUERY t4 eq_ref PRIMARY PRIMARY 4 job_db.t1.City 1 Using index 
2 DEPENDENT SUBQUERY t7 ref PRIMARY PRIMARY 4 job_db.t1.id 1 Using index 
2 DEPENDENT SUBQUERY t3 ref Job Job 4 job_db.t1.id 1 Using index 
2 DEPENDENT SUBQUERY t5 ref PRIMARY PRIMARY 4 job_db.t7.Job 1 Using index 
2 DEPENDENT SUBQUERY t6 ref PRIMARY PRIMARY 4 job_db.t7.Job 2 Using index 
  • 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-05-25T00:18:20+00:00Added an answer on May 25, 2026 at 12:18 am

    Try this:

    SELECT w.id, w.Name, COUNT( tw.Task_id )
    FROM Worker AS w
    LEFT JOIN Task_Worker_Combi AS tw
    ON(
      w.id = tw.Worker_id AND
      EXISTS( SELECT Task_id FROM Task_Location_Combi
                WHERE Task_id = tw.TaskId AND Location_id IN(1, 2) ) AND
      EXISTS( SELECT Task_id FROM Task_Department_Combi
                WHERE Task_id = tw_TaskId AND Department_id IN(3, 4) )
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the updated question: the current query is doing something like: $sql1 =
Updated question: Django is giving me the following sql query: SELECT auth_user.id, auth_user.username, auth_user.first_name,
How do I formulate this query update forge..dimInteg2 set duplicates = (select count(*) from
I have updated a table in a database using mysql query browser. is it
Whenever I execute an update-query, my whole table is updated. What do I have
I want to retrieve the most recently updated value in the table using an
Initial situation: Inside a function I'm caching a query using the cachedWithin attribute. Right
is it possible to get the new/updated _id after the query? example code: $key
How can I print the rows updated by this query in this query: update
I am using a sysdate query in code to obtain the last updated rows

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.