从mysql表中的同一列获取计数?

时间:2021-02-25 08:05:26

I wanted to a count of the same field for different values for example:

我想为不同的值计算相同的字段,例如:

user{user_id, gender}

用户{user_id,性别}

Gender can have obviously male or female :)

性别可以有明显的男性或女性:)

i want to get count for all the males and females i.e.

我想得到所有男性和女性的数量,即

COUNT(male)   COUNT(female)  
   4               16

but im confused because they come from the same gender coloumn thanks

但我很困惑,因为他们来自同一性别coloumn谢谢

4 个解决方案

#1


11  

Try this for row wise result:

尝试以行为结果:

SELECT gender, COUNT(User_id) AS count
FROM User
GROUP BY gender;

Output:

输出:

| gender | count |
|--------|-------|
|      F |     4 |
|      M |     2 |

Try this for row wise result with grand total:

尝试使用总计的行方式结果:

SELECT  (IFNull(gender,'Total')) AS gender,
COUNT(User_id) AS Count
FROM User
GROUP BY gender
WITH rollup;

Output:

输出:

| gender | Count |
|--------|-------|
|      F |     4 |
|      M |     2 |
|  Total |     6 |

Try this for column wise result:

尝试以列方式结果:

SELECT
  COUNT(CASE WHEN gender = 'M' THEN User_id END) AS males,
  COUNT(CASE WHEN gender = 'F' THEN User_id END) AS females,
  COUNT(*) AS Total
FROM User;

Output:

输出:

| males | females | Total |
|-------|---------|-------|
|     2 |       4 |     6 |

See this SQLFiddle

#2


2  

Based on your comment, you want count for both males, females and the total count:

根据您的评论,您需要计算男性,女性和总数:

SELECT sum(case when gender = 'M' then 1 else 0 end) males,
  sum(case when gender = 'F' then 1 else 0 end) females,
  count(*) total
FROM  yourTable

See SQL Fiddle with Demo

请参阅SQL Fiddle with Demo

#3


0  

Try this query -

试试这个查询 -

SELECT
  COUNT(IF(gender = 'M', User_id, NULL) males,
  COUNT(IF(gender = 'F', User_id, NULL) females
FROM
  User

#4


0  

try this: with total count:

试试这个:总数:

Select gender,count(*) as count
From User
Group by gender
with rollup

SQl Fiddle demo

SQl小提琴演示

#1


11  

Try this for row wise result:

尝试以行为结果:

SELECT gender, COUNT(User_id) AS count
FROM User
GROUP BY gender;

Output:

输出:

| gender | count |
|--------|-------|
|      F |     4 |
|      M |     2 |

Try this for row wise result with grand total:

尝试使用总计的行方式结果:

SELECT  (IFNull(gender,'Total')) AS gender,
COUNT(User_id) AS Count
FROM User
GROUP BY gender
WITH rollup;

Output:

输出:

| gender | Count |
|--------|-------|
|      F |     4 |
|      M |     2 |
|  Total |     6 |

Try this for column wise result:

尝试以列方式结果:

SELECT
  COUNT(CASE WHEN gender = 'M' THEN User_id END) AS males,
  COUNT(CASE WHEN gender = 'F' THEN User_id END) AS females,
  COUNT(*) AS Total
FROM User;

Output:

输出:

| males | females | Total |
|-------|---------|-------|
|     2 |       4 |     6 |

See this SQLFiddle

#2


2  

Based on your comment, you want count for both males, females and the total count:

根据您的评论,您需要计算男性,女性和总数:

SELECT sum(case when gender = 'M' then 1 else 0 end) males,
  sum(case when gender = 'F' then 1 else 0 end) females,
  count(*) total
FROM  yourTable

See SQL Fiddle with Demo

请参阅SQL Fiddle with Demo

#3


0  

Try this query -

试试这个查询 -

SELECT
  COUNT(IF(gender = 'M', User_id, NULL) males,
  COUNT(IF(gender = 'F', User_id, NULL) females
FROM
  User

#4


0  

try this: with total count:

试试这个:总数:

Select gender,count(*) as count
From User
Group by gender
with rollup

SQl Fiddle demo

SQl小提琴演示