简体   繁体   English

如何使用函数联接两个MySQL表

[英]How do I Join Two MySQL Tables Using a Function

I need some help joining two table. 我需要一些帮助加入两个表。 I've got: 我有:

my_type_table , which has columns: my_type_table ,具有以下列:

  • type (VARCHAR) 类型(VARCHAR)
  • latitude (decimal) 纬度(十进制)
  • longitude (decimal) 经度(十进制)

...and neighborhood_shapes , which has columns: ...和neighborhood_shapes ,其中包含列:

  • neighborhoods (VARCHAR) 社区(VARCHAR)
  • neighborhood_polygons (geometry) Neighborhood_polygons(几何)

I've got a function called myWithin which checks the latitude and longitude to see whether they are in the neighborhood polygon. 我有一个名为myWithin的函数,该函数检查纬度和经度以查看它们是否在邻域多边形中。 It takes the lat long and the neighborhood shape as parameters. 它以纬度和邻域形状为参数。 The myWithin function returns 0 if the point is not in the polygon and 1 if it is within the polygon. 如果该点不在多边形内,则myWithin函数返回0;如果在多边形内,则返回1。

I can make a select statement as follows: 我可以做出如下选择语句:

SELECT type, latitude, longitude, 'Newport' AS neighborhood
  FROM my_type_table
WHERE myWithin(POINTFROMTEXT( CONCAT( 'POINT(', latitude, ' ', longitude, ')' ) ) , 
               (SELECT neighborhood_shapes.neighborhood_polygons
                  FROM neighborhood_shapes
                 WHERE neighborhood_shapes.neighborhoods = 'Newport')) = 1

The results of this select are for example: 例如,此选择的结果是:

type  |  latitude | longitude | neighborhood
---------------------------------------------
small |  30.3030  | -70.7070  | Newport

My problem is that I have a lot of neighborhoods. 我的问题是我有很多社区。 I don't want to have to input the neighborhood each time. 我不想每次都输入邻居。 Is there a way to remove "Newport"? 有没有办法删除“ Newport”? Basically, I want the function to run on each point and give me the type, latitude, longitude, and which neighborhood the point is in? 基本上,我希望函数在每个点上运行,并提供类型,纬度,经度以及该点位于哪个邻域中?

I could copy the above select and join the select statements with UNION ALL, but it would be a nightmare typing in each neighborhood's name. 我可以复制上面的select并将select语句与UNION ALL联接在一起,但是输入每个邻域的名称将是一场噩梦。 There's got to be a better way. 必须有一个更好的方法。

Any suggestions? 有什么建议么?

i didnt test this of course... but it seems like it could be restructured similar to this: 我当然没有测试过……但是似乎可以像这样重组:

SELECT t.type, t.latitude, t.longitude, s.neighborhood 
  FROM my_type_table t, neighborhood_shapes s
WHERE myWithin(POINTFROMTEXT( CONCAT( 'POINT(', t.latitude, ' ', t.longitude, ')' ) ) ,               s.neighborhood_shapes.neighborhood_polygons ) = 1

Have you tried... 你有没有尝试过...

SELECT DISTINCT type,
                latitude,
                longitude,
                ns.neighborhoods AS neighborhood
    FROM my_type_table,
         neighborhood_shapes ns
    WHERE myWithin(POINTFROMTEXT( CONCAT( 'POINT(', latitude, ' ', longitude, ')' ) ) , 
                   (SELECT neighborhood_shapes.neighborhood_polygons
                        FROM  neighborhood_shapes
                        WHERE neighborhood_shapes.neighborhoods = ns.neighborhoods)) = 1
function getNeighborhood ($neighborhood) {


$sql = "SELECT type, latitude, longitude, 'Newport' AS neighborhood
  FROM my_type_table
WHERE myWithin(POINTFROMTEXT( CONCAT( 'POINT(', latitude, ' ', longitude, ')' ) ) , 
               (SELECT neighborhood_shapes.neighborhood_polygons
                  FROM neighborhood_shapes
                 WHERE neighborhood_shapes.neighborhoods = $neighborhood)) = 1";

$result = /**  Do Query Here**/

return $result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM