从复杂的MySQL中删除重复项选择查询

时间:2017-04-19 21:42:49

标签: php mysql sql

我的所有项目都存储在com_projects表中。每个项目可以有多个位置,这些位置存储在com_location表中。 com_country表只是一个国家/地区名称列表,com_location表用它来提取国家/地区的名称。

在com_project表中,我有一个category_id字段,其值可以是1-4。

下面的SELECT查询是我尝试计算每个国家/地区分配类别的总次数。

---------------------------------------------------------------
| country | category_1 | category_2 | category_3 | category_4 |
---------------------------------------------------------------
| USA     |     20     |      5     |     3      |     0      |
---------------------------------------------------------------
| UK      |     1      |      12    |     0      |     0      |

等...

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

如果项目在不同国家/地区有一个或多个地点,我只希望增加一个类别。我遇到的问题是,许多项目在同一个国家/地区有多个位置,并且人为地增加了结果。

如何调整我的SELECT语句以防止来自同一个国家/地区的重复项?

顺便说一句,我尝试在SELECT之后立即添加DISTINCT并且它没有帮助。

1 个答案:

答案 0 :(得分:0)

我认为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;

这只是检查正在聚合的每个项目是否至少有一个其他国家。