[英]how make mysql query for that case
我讓我的真實案例更簡單。
案子:
它們以點的形式傳遞多條曲線,每條曲線都有最終的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不是他的最終點
psedocode函數 - 需要添加代碼以選擇相同的最終點 :
function findCurvesForSamePointAndSameFinalPoint(pointID){
query="SELECT curve_id FROM curve INNER JOIN point GROUP BY curve_id HAVING point_id="+pointID+";";
return getDATABASEResult(query);
}
Edit2:在線sql用一些數據來測試: http ://sqlfiddle.com/#!2/59e9f/1(現有的查詢沒有用)
謝謝
如果我做對了 它是這樣的:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.