查询并获得三个预期结果

时间:2014-03-28 07:27:18

标签: mysql sql

SELECT
(
    `members`.`id`
    SELECT COUNT(`members`.`id`) FROM `members` WHERE `gender` = 0 AS `Unknown`
    SELECT COUNT(`members`.`id`) FROM `members` WHERE `gender` = 1 AS `Female`
    SELECT COUNT(`members`.`id`) FROM `members` WHERE `gender` = 2 AS `Male`
) FROM `members` INNER JOIN `mapMember`
ON `mapMember`.`id` = `members`.`id`
WHERE `mapMember`.`mapper_id` = 3

我的预期结果:

Unknown     Female      Male
0           1           3

但是我收到SYNTAX错误。广东话'找出问题所在。

我也尝试过:

SELECT COUNT(`members`.id) AS `members`, `gender`  
            FROM `members` INNER JOIN `mapMember`
             ON `mapMember`.`id` = `members`.`id`   
            WHERE `mapMember`.`mapper_id` = 3 GROUP BY `gender` ORDER BY `gender` ASC

这给了我几乎我想要的结果,唯一的区别是如果没有给定性别的成员,那么就不会有0结果。 (没有行)我总是期待三行。

3 个答案:

答案 0 :(得分:1)

SELECT
  sum(if (`gender` = 0, 1,0)) as `Unknown`,
  sum(if (`gender` = 1, 1,0)) as `Female`,
  sum(if (`gender` = 2, 1,0)) as `Male`
FROM `members` INNER JOIN `mapMember`
ON `mapMember`.`id` = `members`.`id`
WHERE `mapMember`.`mapper_id` = 3

答案 1 :(得分:0)

SELECT * FROM
(

    SELECT `members`.`id`,COUNT(`members`.`id`) AS `Unknown` FROM `members` WHERE `gender` = 0 
    UNION
    SELECT `members`.`id`,COUNT(`members`.`id`) AS `Female` FROM `members` WHERE `gender` = 1 
    UNION
    SELECT `members`.`id`,COUNT(`members`.`id`) AS `Male` FROM `members` WHERE `gender` = 2 
) Z INNER JOIN `mapMember`
ON `mapMember`.`id` = `Z`.`id`
WHERE `mapMember`.`mapper_id` = 3

答案 2 :(得分:0)

其他人已经为您提供了解决方案,所以我主要告诉您自己的陈述在哪里出错。

  1. COUNT(column_name)只计算column_name不为null的记录。 membersid不为空,因此您只需计算成员的所有记录。您需要一个where子句,而不是将计数记录限制为相关的成员ID。
  2. 子查询必须在括号中。
  3. 这是你的陈述重写。但这并不好,因为您一次又一次地查询同一个表。我只是想使用你的陈述,只纠正错误:

    SELECT
      `members`.`id`,
      (SELECT COUNT(*) FROM `members` u WHERE `gender` = 0 AND u.id = members.id) AS `Unknown`
      (SELECT COUNT(*) FROM `members` f WHERE `gender` = 1 AND f.id = members.id) AS `Female`
      (SELECT COUNT(*) FROM `members` m WHERE `gender` = 2 AND m.id = members.id) AS `Male`
    FROM `members` INNER JOIN `mapMember`
    ON `mapMember`.`id` = `members`.`id`
    WHERE `mapMember`.`mapper_id` = 3;
    

    现在它在语法上是正确的。但是,由于会员记录只有一个性别,因此您将始终获得0-0-1或0-1-0或1-0-0的记录。因此,您并不真正想要选择成员并拥有每个成员的计数。

    这是一个更好的语句,只查询一次表,计算所有记录而不是每个成员,并通过对mapmember使用IN子句提供更好的可读性。 (您也可以使用EXISTS子句替换IN子句,这有时会更快。)

    select
      sum( case when gender = 0 then 1 else 0 end ) as unknown, 
      sum( case when gender = 1 then 1 else 0 end ) as female, 
      sum( case when gender = 2 then 1 else 0 end ) as male 
    from members
    where id in (select id from mapmember where mapper_id = 3);
    

    (顺便说一句:mapmember id是否真的是一个成员id?看起来很奇怪,有一个名为id的列的表,这不是表本身的id,而是实际上是另一个表的id。)

    编辑:我只是注意到你使用的是MySQL。您可以使用布尔数据类型:

    select
      sum( gender = 0 ) as unknown, sum( gender = 1 ) as female, sum( gender = 2 ) as male 
    from members
    where id in (select id from mapmember where mapper_id = 3);
    

    这不再是标准的SQL,因为它使用了MySQL的增强功能。