0

我有一个 SQL 查询,可以找到购买特定房产类型的每个人的平均年龄。查询返回每个属性类型的平均年龄,但不包括“平均年龄”字段为空的属性类型。我该怎么做呢?

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
4

1 回答 1

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
于 2013-04-28T12:37:30.487 回答