简体   繁体   English

SQL选择数据与多个列匹配的行

[英]SQL select rows where data matches multiple columns

I have multiple pairs of cartesian coordinates (X,Y) calculated by user input (php), which form a range (picture a polygon). 我有通过用户输入(php)计算的多对笛卡尔坐标(X,Y),它们构成一个范围(将一个多边形表示为多边形)。

From my database (600 points), I need to find any points that fall within that range (within the polygon), and exclude any that are outside that range. 从数据库(600个点)中,我需要找到该范围内(多边形内)的所有点,并排除该范围外的任何点。

I would like to do it all in one SQL statement, but I can't seem to get my head around the logic of matching two different values - to two different columns - of the same row. 我想在一个SQL语句中完成所有操作,但是我似乎无法理解将同一行的两个不同值(匹配到两个不同列)进行匹配的逻辑。

I suppose I split the data between two tables and use an inner join? 我想我在两个表之间拆分数据并使用内部联接? But this seems a bit over the top. 但这似乎有点过头了。

I have tried playing with the geometry part of MYSQL (trying to search coord pairs using the "point" datatype), but I cannot see the data I have imported (after a successful import). 我尝试使用MYSQL的几何部分(尝试使用“点”数据类型搜索坐标对),但是我看不到已导入的数据(成功导入后)。 And when I select all data from a row try and display $row["coords"] (which should be the point data), all I get is a bunch of weird ASCII characters and squares... 当我从一行中选择所有数据并显示$ row [“ coords”](应该是点数据)时,我得到的只是一堆奇怪的ASCII字符和正方形...

So hopefully there is an easy way to do it using regular SQL. 因此,希望有一种使用常规SQL的简便方法。

Let say you have table like this: 假设您有这样的表格:

TABLE_1: 表格1:

ID     |    INT
X_POS  |    INT
Y_POS  |    INT 

ID - autoincremented primary key X_POS - X-coordinate of point Y_POS - Y-coordinate of point ID-自动递增的主键X_POS-点的X坐标Y_POS-点的Y坐标

Now you have sample data such as these: 现在,您具有以下示例数据:

ID | X_POS | Y_POS
1  | 3.25  | 1.75
2  |-0.5   | 2.17

etc.. 等等..

Now, you want to select all rows where point are on X-axis between -1.34 and 1.28 and on Y-axis between -5.63 and0.98 现在,您要选择点位于-1.34和1.28之间的X轴上以及-5.63和0.98之间的Y轴上的所有行

You query should be as follows: 您的查询应如下所示:

SELECT * FROM TABLE_1 
WHERE 
(X_POS BETWEEN -1.34 AND 1.28) AND
(Y_POS BETWEEN -5.63 AND 0.98)

You can test this sample here: SQLFIDDLE 您可以在此处测试此示例: SQLFIDDLE

UPDATE: 更新:

You should definitely use MySQL Spatial Extension 您绝对应该使用MySQL Spatial Extension

I have tried playing with the geometry part of MYSQL (trying to search coord pairs using the "point" datatype), but I cannot see the data I have imported 我尝试使用MYSQL的几何部分(尝试使用“点”数据类型搜索坐标对),但是看不到导入的数据

Please provide more details about your tables (SHOW CREATE TABLE) and the way you used to import data. 请提供有关表(SHOW CREATE TABLE)以及用于导入数据的方式的更多详细信息。

Then I may help you. 那我可以帮你。

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

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