开发者

MySQL Query Join and Count Query

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 |开发者_JS百科 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


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)


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  


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 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜