如何为这种情况编写MySQL查询

3

我将简化我的实际情况。

情况:

有多条曲线它们经过一些点,每个点都有最终的1个点。在数据库中,曲线的最终点表示为最大的point_order值。

应该找到通过特定点并具有相同终点(相同的point_id)的曲线。

情况(表):

点表:

point_id|x|y

编辑:

curve_points表示例 - 查找所有具有相同point_id=80和相同终点的曲线:

id|curve_id|point_id|point_order
  |119     |6       |12
  |119     |80      |9
  |119     |1000    |1
  |76      |80      |7
  |76      |6       |9
  |76      |2       |2
  |90      |80      |7
  |90      |6       |9
  |90      |99      |15

输出结果应为:
  |curve_id|
  |119     | 
  |76      |

因为曲线119,76有相同的终点=6,并且有相同的80点。曲线90不行,因为点6不是它的终点。

伪代码函数 - 需要添加代码以选择相同的终点

function findCurvesForSamePointAndSameFinalPoint(pointID){
    query="SELECT curve_id FROM curve INNER JOIN point GROUP BY curve_id HAVING point_id="+pointID+";";
    return getDATABASEResult(query);  
}

编辑2:

在线SQL,带有一些测试数据:http://sqlfiddle.com/#!2/59e9f/1(那里现有的查询不起作用)。

谢谢。


你的意思是最终点和传递点是相同的,还是你需要曲线它们的最终点相同? - Maryam Arshi
请进一步阐述您的问题。它仍然不清楚。 - Rachcha
感谢您的评论,我编辑了问题 - 它包含输入和输出的示例。 - Ben
2个回答

1
如果我理解正确,它大概是这样的:

SQLFiddle演示

select distinct c1.curve_id,(select point_id from curve t1
       where t1.curve_id=c1.curve_id 
       order by point_order desc 
       limit 1)
TheLastPoint

from curve c1
join curve c2 on
(select point_id from curve t1
       where t1.curve_id=c1.curve_id 
       order by point_order desc 
       limit 1)
=
(select point_id from curve t2 
       where t2.curve_id=c2.curve_id 
       order by point_order desc 
       limit 1)
And c1.curve_id<>c2.curve_id

where c1.curve_id in (select curve_id from curve where point_id=80)
      and 
      c2.curve_id in (select curve_id from curve where point_id=80)
order by TheLastPoint,c1.curve_id

现在的查询已根据您的数据进行了修复。 - valex
为什么不呢?它根据您的条件选择所有成对曲线。在您的示例中,有两组包含点-80且具有相同终点的曲线组。76,119具有终点6,第二组70,90,92具有终点99。 - valex

0

首先我想问的是,曲线表如何与点表建立关系?必须有冗余的Curve_ids来映射它们与Point表。

如果可以更改您的数据库结构,您可以使用内置类如PointCurve的MySQL Geometry。您可以使用内置功能检查两条曲线是否相交等等。

我发现这个相关。


1
谢谢,我已经修正了错误 - 应该使用curve_points表而不是仅使用curve表。 - Ben
现有的数据库无法更改架构。 - Ben

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接