Calculating distance (pythagoras) and running count in sql query

半腔热情 提交于 2019-11-29 16:59:24

1., 2. To bring tables together and perform operations between them, you need to use Join http://dev.mysql.com/doc/refman/5.0/en/join.html otherwise your formula is correct. To create it as a column in your query, just write it in the projection(select) part. Example:

select 
population_postcodes.*, 
target_postcodes.*, 
SQRT( POW(population_postcodes.longitude- target_postcodes.longitude, 2) + POW(population_postcodes.latitude-target_postcodes.latitude, 2) ) as distance
from population_postcodes JOIN target_postcodes

points 2 bis. End with Order by column_name asc/desc http://dev.mysql.com/doc/refman/5.0/en/sorting-rows.html

point 3. Write everything as a sub-query, and select only what you need in the top query. Also look at HAVING http://dev.mysql.com/doc/refman/5.0/en/subqueries.html http://dev.mysql.com/doc/refman/5.0/en/group-by-extensions.html

point 4. look at ways to create tables and apply what you nearned

create table mytablename
select ... my projection columns
from ...

http://dev.mysql.com/doc/refman/5.1/en/create-table.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!