2013-02-19 68 views
3

我讓我的真實情況更簡單。如何讓MySQL查詢這種情況下

的情況下:

有多個曲線它們在點傳遞和他們每個人都有在數據庫作爲曲線的最大point_order值表示最後1個point.The最後一點。

應該發現,通過在特定的點,並且具有相同的最終點(同point_id)

的情況下(表)曲線:

點表:

編輯:

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(不存在查詢有作品)

感謝

+0

你的意思是最後一點,並通過點是相同的,或者需要曲線,該曲線最終點是一樣的嗎? – 2013-02-19 10:54:54

+0

請進一步闡述你的問題。它仍然不清楚 – Rachcha 2013-02-19 10:58:19

+0

感謝您的意見我編輯的問題 - 它包含輸入和輸出的例子。 – Yosef 2013-02-19 11:46:00

回答

1

如果我是正確的。它是這樣的:

我想問

SQLFiddle demo

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 
+0

謝謝,請參閱我的編輯 – Yosef 2013-02-19 12:48:00

+0

現在根據您的數據修復查詢。 – valex 2013-02-19 12:59:50

+0

如果我添加更多曲線,查詢無法正常工作:http://sqlfiddle.com/#!2/59e9f/1 – Yosef 2013-02-19 16:00:00

0

第一件事,曲線表是如何創建與點表的關係?必須有REDUNDANT Curve_ids才能將它們與Point表進行映射。

如果可以更改數據庫結構,那麼可以使用MySQL Geometry,它內置了類如PointCurve。您可以檢查兩條曲線crosses以及更多使用內置功能的曲線。

I found this one related.

+1

謝謝,我修復了錯誤 - curve_points表,而不是曲線表 – Yosef 2013-02-19 11:55:27

+0

它存在的數據庫不能改變模式 – Yosef 2013-02-19 16:33:26