[英]Removing Duplicates from complex MySQL Select Query
All my projects are stored in the com_projects table. 我的所有项目都存储在com_projects表中。 Each project can have multiple locations, which are stored in the com_location table. 每个项目可以有多个位置,这些位置存储在com_location表中。 The com_country table is just a list of country names, which is use by the com_location table to pull in the name of the countries. com_country表只是一个国家/地区名称列表,com_location表用它来提取国家/地区的名称。
In the com_project table I have a category_id field that can have a value from 1-4. 在com_project表中,我有一个category_id字段,其值可以是1-4。
The SELECT query below is my attempt to count the total number of times a category is assigned per country. 下面的SELECT查询是我尝试计算每个国家/地区分配类别的总次数。
---------------------------------------------------------------
| country | category_1 | category_2 | category_3 | category_4 |
---------------------------------------------------------------
| USA | 20 | 5 | 3 | 0 |
---------------------------------------------------------------
| UK | 1 | 12 | 0 | 0 |
etc.... 等等....
SELECT b.country_id, c.name,
SUM(case when a.category_id = 1 then 1 else 0 end) as category_1,
SUM(case when a.category_id = 2 then 1 else 0 end) as category_2,
SUM(case when a.category_id = 3 then 1 else 0 end) as category_3,
SUM(case when a.category_id = 4 then 1 else 0 end) as category_4
FROM com_project a
Inner JOIN com_location b
ON a.id = b.project_id
INNER JOIN com_country c
ON c.id = b.country_id
WHERE a.state = 1
AND b.state = 1
GROUP BY b.country_id
I would only like a category to be incremented if a project has one or more locations in a different countries. 如果项目在不同的国家/地区有一个或多个位置,我只希望增加一个类别。 The problem I have is that many projects have multiple location in the same country and it's artificially increasing the results. 我遇到的问题是,许多项目在同一个国家/地区有多个位置,并且人为地增加了结果。
How can I tweak my SELECT statement to prevent duplicates from the same country? 如何调整我的SELECT语句以防止来自同一个国家/地区的重复?
By the way I tried adding DISTINCT immediately after the SELECT and it didn't help. 顺便说一句,我尝试在SELECT之后立即添加DISTINCT并且它没有帮助。
I think a where
clause will do for filtering out projects that are always in one country: 我认为where
子句可以过滤掉总是在一个国家/地区的项目:
SELECT l.country_id, c.name,
SUM(p.category_id = 1) as category_1,
SUM(p.category_id = 2) as category_2,
SUM(p.category_id = 3) as category_3,
SUM(p.category_id = 4) as category_4
FROM com_project p Inner JOIN
com_location l
ON p.id = l.project_id INNER JOIN
com_country c
ON c.id = l.country_id
WHERE p.state = 1 AND l.state = 1 AND
EXISTS (SELECT 1
FROM com_location l2
WHERE l2.project_id = l.project_id AND l2.country <> l.country
)
GROUP BY l.country_id, c.name;
This just checks that each project being aggregated has at least one other country. 这只是检查正在聚合的每个项目是否至少有一个其他国家/地区。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.