我有一个SQL查询,它查找购买特定属性类型的每个人的平均年龄。查询返回每种属性类型的平均年龄,但不包括“平均年龄”的属性类型。 field为null。我该怎么做呢?
SELECT round(avg(round((datediff(now(),dateofbirth)/365),0))) as 'Average Age', propertyname
from apartment, buyer
WHERE buyer.propertyID = apartment.propID
group by propertyname
UNION
SELECT round(avg(round((datediff(now(),dateofbirth)/365),0))) as 'Average Age', propertyname
from house, buyer
WHERE buyer.propertyID = house.propID
group by propertyname
答案 0 :(得分:0)
尝试:
SELECT round(avg(round((datediff(now(),dateofbirth)/365),0))) as 'Average Age',
propertyname
from apartment
LEFT JOIN buyer ON buyer.propertyID = apartment.propID
group by propertyname
UNION ALL
SELECT round(avg(round((datediff(now(),dateofbirth)/365),0))) as 'Average Age',
propertyname
from house
LEFT JOIN buyer ON buyer.propertyID = house.propID
group by propertyname