MySQL Average:使语句更高效
我正在使用这个 MySQL 语句来查找社区的平均房产评估。问题是我必须为每个邻域添加一条 SELECT 语句——而且有很多邻域。有没有办法不必指定“RIVER FRONT”或“OLD TOWN”?有更有效的方法吗?
SELECT AVG(property_table.assessment)
FROM property_table, neighborhood_table
WHERE property_table.id = neighborhood_table.id AND neighborhood_table.neighborhood = 'RIVER FRONT'
UNION ALL
SELECT AVG(property_table.assessment)
FROM property_table, neighborhood_table
WHERE property_table.id = neighborhood_table.id AND neighborhood_table.neighborhood = 'OLD TOWN'
I'm using this MySQL statement to find a neighborhood's average property assessment. The problem is that I have to add a SELECT statement for each neighborhood-- and there are a lot of neighborhoods. Is there a way to do it without having to specify 'RIVER FRONT' or 'OLD TOWN'? Is there a more efficient way?
SELECT AVG(property_table.assessment)
FROM property_table, neighborhood_table
WHERE property_table.id = neighborhood_table.id AND neighborhood_table.neighborhood = 'RIVER FRONT'
UNION ALL
SELECT AVG(property_table.assessment)
FROM property_table, neighborhood_table
WHERE property_table.id = neighborhood_table.id AND neighborhood_table.neighborhood = 'OLD TOWN'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个标准 SQL 聚合语句,显示所有社区的平均值
左连接允许您在邻域中不存在任何属性的情况下获取零值。
This is an standard SQL aggregate statement and shows averages for all neighbourhoods
The left join allows you to get zero values where no property exists in a neighbourhood.