Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何计算数据库中两个不同列的值?
例子:
我有一家桌子医院,里面有一列,hosptal_id 和 sex
现在我想要一个在医院内按类型(男性或女性)计算性别列的结果
我想要结果像..
hospital_id ------ F ------ M
1 ------ 14----- 45
2 ------ 23----- 49
3 ------ 29-------- 76
,我怎样才能得到这个结果?
有人可以帮我吗?
SELECT id,SUM(IF(sex='M',1,0)) AS 'male', SUM(IF(sex='F',1,0)) AS 'female' FROM hospital GROUP BY id
试试这样的查询——
SELECT COUNT(DISTINCT column_name) AS some_alias FROM table_name