0

我有以下五个表:

 company        type    branch      company_type       company_branch
 -------        ----    ------     -------------       --------------
  c_id          t_id     b_id       c_id | t_id         c_id | b_id     
  ----          ---      ----       -----------        --------------
   1             1        1            1  1              1     1
   2             2        2            1  2              1     2
                 3        3            2  1              2     1
                                       2  3              2     3

以下两个查询工作正常:

SELECT DISTINCT `co`.`c_id`
FROM (`company` co)
JOIN `company_type` t ON `t`.`c_id` = `co`.`c_id`
WHERE `co`.`status` = 1
GROUP BY `co`.`c_id`
HAVING sum(if(`t`.`t_id` in (1,2),1,0)) = 2       (---> returns 1 ) Correct !

SELECT DISTINCT `co`.`c_id`
FROM (`company` co)
JOIN `company_branch` b ON `b`.`c_id` = `co`.`id`
WHERE `co`.`status` = 1
GROUP BY `co`.`id`
HAVING sum(if(`b`.`b_id` in (1,2),1,0)) = 2        (---> returns 1 ) Correct !

但是当我尝试像下面这样组合它们时,它给了我一个空的结果:

SELECT DISTINCT `co`.`c_id`
FROM (`company` co)
JOIN `company_branch` b ON `b`.`c_id` = `co`.`id`
JOIN `company_type` t ON `t`.`c_id` = `co`.`id`
WHERE `co`.`status` = 1
GROUP BY `co`.`id`
HAVING sum(if(`b`.`b_id` in (1,2),1,0)) = 2 AND sum(if(`t`.`t_id` in (1,2),1,0)) = 2

我的猜测是这两个条件之间的 AND 是罪魁祸首!有人有线索吗??或解决方法?

预期结果:1

4

3 回答 3

2

可能是因为您无意中获得了笛卡尔连接,从而导致了 4 条记录。每个分支和类型两个。我的猜测是您的计数是每个 4... 可以肯定的是,尝试

SELECT `co`.`c_id`,
       sum(if(`b`.`b_id` in (1,2),1,0)) as BranchCount,
       sum(if(`t`.`t_id` in (1,2),1,0)) as TypeCount
   FROM (`company` co)
      JOIN `company_branch` b ON `b`.`c_id` = `co`.`id`
      JOIN `company_type` t ON `t`.`c_id` = `co`.`id`
   WHERE `co`.`status` = 1
   GROUP BY `co`.`id`

要修复它,我会进行如下调整:

select co.C_ID
   from
      company co
         join company_branch b 
            on co.id = b.c_id
           and b.b_id in ( 1, 2 )
         join company_type t 
            on co.id  = t.c_id
           and t.t_id in ( 1, 2 )
   where
      co.status = 1
   GROUP BY 
      co.id
   HAVING
           count( distinct b.b_id ) = 2
       AND count( distinct t.t_id ) = 2
于 2012-04-17T15:45:06.983 回答
0

此查询应该满足您的要求。不是在 HAVING 子句中指定过滤条件,而是首先计算每个 co_id 的总和,然后使用子查询对其应用 WHERE 子句过滤条件。这应该可以工作。

SELECT Z.* FROM
(
SELECT `co`.`c_id`,
       sum(if(`b`.`b_id` in (1,2),1,0)) as BranchCount,
       sum(if(`t`.`t_id` in (1,2),1,0)) as TypeCount
   FROM (`company` co)
      JOIN `company_branch` b ON `b`.`c_id` = `co`.`id`
      JOIN `company_type` t ON `t`.`c_id` = `co`.`id`
   WHERE `co`.`status` = 1
   GROUP BY `co`.`id`
) Z
WHERE Z.BranchCount=2 AND Z.TypeCount=2;
于 2012-04-17T15:51:14.857 回答
0

顺便说一句,您发布的查询有一些错误,所以我正在尝试发布正确的查询,以便如果有人想尝试,他们可以做到。第一个: SELECT DISTINCT coc_id FROM ( companyco) 加入company_typet ON tc_id= coc_id 分组依据coc_id HAVING sum(if( t. t_idin (1,2),1,0)) = 2 第二个:

SELECT DISTINCT `co`.`c_id`
FROM (`company` co)
JOIN `company_branch` b ON `b`.`c_id` = `co`.`c_id`
GROUP BY `co`.`c_id`
HAVING sum(if(`b`.`b_id` in (1,2),1,0)) = 2

我已经排除了状态列,因为我没有在我的表结构中使用它。

现在我不确定您的业务逻辑是什么,但我正在提出您的最终 SQL,如下所示:

SELECT DISTINCT `co`.`c_id`,sum(if(`cb`.`b_id` in (1,2),1,0)) cb_sum,sum(if(`ct`.`t_id` in (1,2),1,0)) ct_sum
FROM (`company` co)
LEFT JOIN `company_branch` cb ON `cb`.`b_id` = `co`.`c_id`
LEFT JOIN `company_type` ct ON `ct`.`t_id` = `co`.`c_id`
GROUP BY `co`.`c_id`
HAVING sum(if(`cb`.`b_id` in (1,2),1,0)) = 2 AND sum(if(`ct`.`t_id` in (1,2),1,0)) = 2

现在,如果您运行没有 HAVING 子句的 SQL,您应该会看到如下输出:

c_id      cb_sum       ct_sum
1         4            4
2         1            1

因此,如果您将条件更改为“> 2”而不是“= 2”,您应该得到您想要的,但另一个选项是您使用 UNION 组合您的结果集,如下所示:

SELECT DISTINCT `co`.`c_id`
FROM (`company` co)
JOIN `company_type` t ON `t`.`c_id` = `co`.`c_id`
GROUP BY `co`.`c_id`
HAVING sum(if(`t`.`t_id` in (1,2),1,0)) = 2
UNION
SELECT DISTINCT `co`.`c_id`
FROM (`company` co)
JOIN `company_branch` b ON `b`.`c_id` = `co`.`c_id`
GROUP BY `co`.`c_id`
HAVING sum(if(`b`.`b_id` in (1,2),1,0)) = 2
于 2012-04-17T15:57:12.480 回答