views:

67

answers:

3

Hi,

I'm trying to pull values from a database for a web app where a moderator can add companies to a list of specified industries. This request needs to pull each industry's name along with a count of attached active companies, as an overview to the moderator.

These are my tables:

companies

 ____________________________________
| id |        company       | active |
|---------------------------|--------|
| 12 | Ton-o-Bricks Haulage |    0   |
| 16 | Roofs 'n' Walls      |    1   |
| 23 | Handy Services       |    1   |
| 39 | Carpentharry         |    1   |
|---------------------------|--------|

industries
 ________________________
| id |   industry  | mod |
|------------------|-----|
|  2 | Roofing     |  2  |
|  4 | Carpentry   |  2  |
|  7 | Handyman    |  2  |
|  8 | Haulage     |  2  |
|  9 | Electrician |  2  |
|------------------|-----|

links
 ___________________________
| id | industry | company  |
|--------------------------|
|  1 |     2    |    23    |
|  2 |     4    |    16    |
|  3 |     4    |    39    |
|  4 |     7    |    23    |
|  5 |     2    |    16    |
|  6 |     8    |    12    |
|--------------------------|

This query works but does not account for inactive companies:

SELECT industries.id, industries.industry, count(links.id) as count FROM industries LEFT JOIN links on links.industry=industries.id WHERE industries.mod=2 GROUP BY industries.id

// -Results =======

2   Roofing     2
4   Carpentry   2
7   Handyman    1
8   Haulage     1
9   Electrician 0

I need it to pull the counts for active companies only, but when I try this I get strange results:

SELECT industries.id, industries.industry, count(links.id) as count FROM industries LEFT JOIN links on links.industry=industries.id, companies WHERE industries.mod=2 AND companies.active=1 GROUP BY industries.id

// -Results =======

2   Roofing     6
4   Carpentry   6
7   Handyman    3
8   Haulage     3
9   Electrician 0

I know i'm missing something simple, I just can't figure out what

Thanks, Steven

+1  A: 

The 2nd query (for active records only) is doing a cross join with Companies table.

Try this - afraid i didnt test it but should work :

SELECT industries.id, industries.industry, count(links.id) as count FROM industries 
LEFT JOIN links on links.industry=industries.id 
INNER JOIN companies on company.id = links.company 
WHERE industries.mod=2 
AND companies.active=1
GROUP BY industries.id 

EDIT :

Added a query that should take care for case with Industry with O count

SELECT industries.id, industries.industry, count(x.id) 
FROM industries JOIN
(
    SELECT links.id, links.industry, company.id 
    FROM companies  
    INNER JOIN links on links.company  = companies.id
    WHERE companies.active=1 
) x
ON industries.id = x.industry  
AND industries.mod=2  
GROUP BY industries.id  
InSane
Great thank you In Sane; That's working, but it's not returning a 0 count for industries without any companies (in this case Electrician, Haulage [Ton-o-Bricks Haulage is inactive]). I've tried adding IFNULL(COUNT(*),0) but that doesn't do the trick
steveneaston
How about replacing the INNER JOIN companies with LEFT JOIN companies.Can you try it out ?
InSane
I get the same results with LEFT JOIN (with and without the IFNULL(COU...), no fields returned with 0 values
steveneaston
hmm..i thought so..right after i posted it... my bad!! Its the AND filter that is causing the problem...the LEFT join wont work..
InSane
Thank you also for your help In Sane, much appreciated.
steveneaston
A: 

Try this:

SELECT industries.id, industries.industry, count(links.id) as count FROM industries 
LEFT JOIN links on links.industry=industries.id 
LEFT JOIN links.company = companies.id 
WHERE industries.mod=2 AND companies.active=1 
GROUP BY industries.id 
Sidharth Panwar
A: 

You may want to try the following:

SELECT      i.id, i.industry, count(l.id) as count
FROM        industries i
LEFT JOIN   (
               SELECT  l.industry, l.id
               FROM    links l
               JOIN    companies c ON (l.company = c.id AND c.active = 1)
            ) l ON (l.industry = i.id)
WHERE       i.mod = 2
GROUP BY    i.id, i.industry;

It should return the following result:

+------+-------------+-------+
| id   | industry    | count |
+------+-------------+-------+
|    2 | Roofing     |     2 |
|    4 | Carpentry   |     2 |
|    7 | Handyman    |     1 |
|    8 | Haulage     |     0 |
|    9 | Electrician |     0 |
+------+-------------+-------+
5 rows in set (0.00 sec)
Daniel Vassallo
Incredible Daniel, thank you so much.
steveneaston