Mysql 我可以使用一个查询来计算SQL中的两列吗
我有Mysql 我可以使用一个查询来计算SQL中的两列吗,mysql,sql,Mysql,Sql,我有 office office_id name --------- ----------------- 1 office1 2 office2 3 office3 person uid office_id age gender --------------------------- 1 1 20 male 2 1 20 female 3 1 20
office
office_id name
--------- -----------------
1 office1
2 office2
3 office3
person
uid office_id age gender
---------------------------
1 1 20 male
2 1 20 female
3 1 20 male
4 1 21 male
5 2 20 male
6 3 20 male
我是否可以使用一个查询来获取
office_id name age_20 male
-----------------------------
1 office1 3 3
2 office2 1 1
3 office3 1 1
是的,你可以。MySQL支持布尔算术,我认为这是最短的方法。如果您想要更友好的RDBMS,请在age=20时使用
CASE,然后使用1 ELSE 0 END
SELECT a.office_ID,
a.name,
SUM(age = 20) age_20,
SUM(gender = 'Male') male
FROM office a
LEFT JOIN person b
ON a.Office_ID = b.office_ID
GROUP BY a.office_ID, a.name
Hive
中进行吗?对不起,我不熟悉Hive。