嘿,伙计们,原谅我这个问题太简单了。我基本上想在一个数据库中统计一下男性和女性的数量。因此,我知道有两个简单的查询可以完成此任务,例如:
select count(*) from table where gender='male'
select count(*) from table where gender='female'
但是,这似乎非常低效,因为我知道下面的查询都是同一个查询:
select count(*) from table where gender='female'
select count(*) from table where gender<>'male'
有没有一种最佳的方法来检索这些信息,而不必遍历数据库中的每一行两次?
最佳答案
您还可以使用分组方式:
SELECT gender, Count(1) as genderCount
FROM table
GROUP BY gender
这将导致如下结果:
gender genderCount
Male 10
Female 15