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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:37:03+00:00 2026-06-10T06:37:03+00:00

I have a query that looks something like: Select tradesmen.id, (SELECT COUNT(quotes.id) FROM quotes

  • 0

I have a query that looks something like:

Select 
    tradesmen.id,
    (SELECT COUNT(quotes.id) FROM quotes WHERE quotes.tradesman_id = tradesmen.id) AS quoted
From
    tradesmen;

So basically there is a subquery for every row in the database (50,000+).
Now for each trademan there could be around 1,000 – 2,000 quotes.

So I could either use this sub query to count them.

Or,

I could do the query to get all the tradesmen.

select tradesman.id from tradesmen;

Do one query to get all the quotes counts

select tradesman_id as id, count(quotes.id) as quotes from quotes group by tradesman_id;

Then loop over each tradesman and pull out of the array the count for each tradesman.

How fast is mysql? would the second method provide significant benefit or is either method going to be acceptable?

For general reference my actual query is:

SELECT 
    tradesmen.*, 
    regions.name AS region_name, 
    GROUP_CONCAT(ptypes_tradesmen.ptype_id SEPARATOR '|') AS ptype_ids, 
    (SELECT 
         COUNT(quotes.id) 
     FROM 
         quotes 
     WHERE 
         quotes.tradesman_id = tradesmen.id
    ) AS quoted, 
    (SELECT 
         COUNT(quote_intentions.id) 
     FROM 
         quote_intentions 
     WHERE 
         quote_intentions.tradesman_id = tradesmen.id
    ) AS intended, 
    (SELECT 
         COUNT(quotes.id) FROM quotes 
     WHERE 
         quotes.tradesman_id = tradesmen.id 
         AND quotes.accepted = 1
    ) AS awarded
FROM 
    (`tradesmen`)
LEFT JOIN `regions` ON `regions`.`id` = `tradesmen`.`region_id`
LEFT JOIN `ptypes_tradesmen` ON `ptypes_tradesmen`.`tradesman_id` = `tradesmen`.`id`
GROUP BY `tradesmen`.`id`

Update

Using the answer from ctrahey I have changed the query.

So, we have three versions of the query now..

ctraheys:

SELECT 
    tradesmen.*, 
    regions.name AS region_name, 
    GROUP_CONCAT(ptypes_tradesmen.ptype_id SEPARATOR '|') AS ptype_ids, 
    COUNT(quotes.id) AS quoted,
    COUNT(quote_intentions.id) AS intended,
    COUNT(NULLIF(quotes.accepted, 0)) AS awarded
FROM (`tradesmen`)
LEFT JOIN `regions` ON `regions`.`id` = `tradesmen`.`region_id`
LEFT JOIN `ptypes_tradesmen` ON `ptypes_tradesmen`.`tradesman_id` = `tradesmen`.`id`
LEFT JOIN quotes ON quotes.tradesman_id = tradesmen.id
LEFT JOIN quote_intentions ON quote_intentions.tradesman_id = tradesmen.id
GROUP BY `tradesmen`.`id`

My modified version:

SELECT 
    t.*, 
    r.name AS region_name, 
    GROUP_CONCAT(p.ptype_id SEPARATOR "|") AS ptype_ids, 
    COUNT(q.id) as quoted, 
    COUNT(i.id) as intended, 
    COUNT(NULLIF(q.accepted, 0)) as awarded
FROM (tradesmen t)
LEFT JOIN regions r ON r.id = t.region_id
LEFT JOIN quotes q ON t.id = q.tradesman_id
LEFT JOIN quote_intentions i ON t.id = i.tradesman_id
LEFT JOIN ptypes_tradesmen p ON p.tradesman_id = t.id
GROUP BY t.id

The original:

SELECT 
    tradesmen.*, 
    regions.name AS region_name, 
    GROUP_CONCAT(ptypes_tradesmen.ptype_id SEPARATOR '|') AS ptype_ids, 
    (SELECT 
         COUNT(quotes.id) 
     FROM 
         quotes 
     WHERE 
         quotes.tradesman_id = tradesmen.id
    ) AS quoted, 
    (SELECT 
         COUNT(quote_intentions.id) 
     FROM 
         quote_intentions 
     WHERE 
         quote_intentions.tradesman_id = tradesmen.id
    ) AS intended, 
    (SELECT 
         COUNT(quotes.id) FROM quotes 
     WHERE 
         quotes.tradesman_id = tradesmen.id 
         AND quotes.accepted = 1
    ) AS awarded
FROM 
    (`tradesmen`)
LEFT JOIN `regions` ON `regions`.`id` = `tradesmen`.`region_id`
LEFT JOIN `ptypes_tradesmen` ON `ptypes_tradesmen`.`tradesman_id` = `tradesmen`.`id`
GROUP BY `tradesmen`.`id`

and whilst they are all returning almost the same results there are differences in the last four fields (hence I am removing all other rows from the results array here).

From original query (correct results):

array('id' => '53',
  'ptype_ids' => '58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68',
  'quoted' => '6',
  'intended' => '14',
  'awarded' => '3'),

From ctrahey’s query:

array('id' => '53',
  'ptype_ids' => '58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|58|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|2|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|7|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|17|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|40|40|40|40|40|4',
  'quoted' => '2016',
  'intended' => '2016',
  'awarded' => '1008'),

From my modified query:

array('id' => '53',
  'ptype_ids' => '58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46|13|67|8|75|59|23|9|31|71|24|68|58|2|7|17|1|40|52|4|74|66|19|15|46',
  'quoted' => '2016',
  'intended' => '2016',
  'awarded' => '1008'),
  • 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-10T06:37:04+00:00Added an answer on June 10, 2026 at 6:37 am

    Neither! Do a proper JOIN (where the real value in an RDBMS is):

    SELECT 
      tradesmen.id,
      COUNT(quotes.id) as quoted
    FROM 
      tradesmen
      LEFT JOIN quotes ON quotes.tradesman_id = tradesmen.id
    GROUP BY tradesmen.id
    

    This query will be exactly what you need and lightening fast!

    edit your real query

    The only caveat needed in your real query is the NULLIF bit inside the count of accepted quotes, as (IIRC) COUNT() will count false/0, but not NULL.

    SELECT 
        tradesmen.*, 
        regions.name AS region_name, 
        GROUP_CONCAT(ptypes_tradesmen.ptype_id SEPARATOR '|') AS ptype_ids, 
        COUNT(quotes.id) AS quoted
        COUNT(quote_intentions.id) AS intended
        COUNT(NULLIF(quotes.accepted, 0)) AS awarded
    FROM (`tradesmen`)
    LEFT JOIN `regions` ON `regions`.`id` = `tradesmen`.`region_id`
    LEFT JOIN `ptypes_tradesmen` ON `ptypes_tradesmen`.`tradesman_id` = `tradesmen`.`id`
    LEFT JOIN quotes ON quotes.tradesman_id = tradesmen.id
    LEFT JOIN quote_intentions ON quote_intentions.tradesman_id = tradesmen.id
    GROUP BY `tradesmen`.`id`
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple MySQL query that looks something like this SELECT * FROM
I have a query that looks something like SELECT to_number FROM sent_texts WHERE to_number
I have a Linq query that looks something like this: var myPosse = from
I have a Linq query that looks something like this: var query = from
I have a query that looks like this SELECT * from myTable WHERE Date
I have a query that looks a bit like this: SELECT weekEnd, MAX(timeMonday) FROM
I have a query that looks something like this: select xmlelement(rootNode, (case when XH.ID
I have a query that looks like this: SELECT OrganizationName, OrganizationID, ReceivableStatus, InvoiceFee FROM
I have a query that looks like this: var ChangesOthersResult = surveyResponseRepository.Query.Select(r => r.ChangesOthers);
I have a query that looks like SELECT P.Column1, P.Column2, P.Column3, ... ( SELECT

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.