MySQL查询加入和计数查询

时间:2010-09-07 10:18:54

标签: mysql join count left-join

我正在尝试从数据库中为Web应用程序提取值,主持人可以将公司添加到指定行业的列表中。此请求需要提取每个行业的名称以及附加活跃公司的数量,作为主持人的概述。

这些是我的表格:

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    |
|--------------------------|

此查询有效,但未考虑无效公司:

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

我需要它来提取活跃公司的数量,但是当我尝试这个时,我得到了奇怪的结果:

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

我知道我错过了一些简单的东西,我只是想不出来是什么

谢谢, 史蒂芬

3 个答案:

答案 0 :(得分:3)

您可能需要尝试以下操作:

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;

它应该返回以下结果:

+------+-------------+-------+
| 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)

答案 1 :(得分:1)

第二个查询(仅限活动记录)正在与公司表进行交叉连接。

试试这个 - 害怕我没有测试它但应该工作:

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 

编辑:

添加了一个查询,该查询应该关注具有O计数的行业

的案例
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  

答案 2 :(得分:0)

试试这个:

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