RETURN 1 table 中的男性数量和女性数量使用 MySQL

RETURN the amount of males and the amount of females in 1 table using MySQL

我需要使用 MySQL RETURN 1 table 中的男性数量和女性数量。我创建了一个 return a tables 包含男性和女性行的查询,但我的列未被填充。结果=0;

这是我的查询。我得到了 table 但它没有被填充

SELECT COUNT(gender) AS 'Female', COUNT(gender) AS 'Male'
FROM customers
WHERE gender = 'female' AND 'male';

任何建议,

这应该有效:

SELECT
  SUM(IF(gender = 'female', 1, 0)) AS 'Female',
  SUM(IF(gender = 'male', 1, 0)) AS 'Male'
FROM customers

IF 根据性别是否为女性(分别为男性)为您提供 1 或 0 的值,然后您只需将这些 0 和 1 相加即可得到总计数。

您也可以执行 CASE 语句:

SELECT
    SUM(CASE WHEN gender = 'female' THEN 1 ELSE 0 END) AS 'Female',
    SUM(CASE WHEN gender = 'male' THEN 1 ELSE 0 END) AS 'Male'
FROM customers

由于 MySQL 将 true 等同于 1,将 false 等同于 0,您可以使用更短的版本:

SELECT
  SUM(gender = 'female') AS `Female`,
  SUM(gender = 'male')   AS `Male`
FROM customers

SQL FIDDLE