MySQL 查询连接和计数查询
我正在尝试从网络应用程序的数据库中提取值,其中主持人可以将公司添加到指定行业的列表中。此请求需要提取每个行业的名称以及附属活跃公司的数量,作为主持人的概述。
这些是我的表格:
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
我知道我错过了一些简单的东西,我只是不知道什么
谢谢, 史蒂文
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能想尝试以下操作:
它应该返回以下结果:
You may want to try the following:
It should return the following result:
第二个查询(仅适用于活动记录)正在与 Companies 表进行交叉连接。
试试这个 - 恐怕我没有测试它,但应该可以工作:
编辑:
添加了一个查询,应该注意 O 计数行业的情况
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 :
EDIT :
Added a query that should take care for case with Industry with O count
试试这个:
Try this: