2011-08-26 123 views
0

我有以下的表與血管跟蹤數據庫模式用於schdual容器跟蹤數據庫模式

CREATE TABLE IF NOT EXISTS `string_ports` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `string_id` int(10) NOT NULL, 
    `port_id` int(11) NOT NULL, 
    `port_order` int(10) NOT NULL, 
    `arriv_date` datetime NOT NULL, 
    `dep_date` datetime NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 CHECKSUM=1 DELAY_KEY_WRITE=1 AUTO_INCREMENT=1 ; 

的string_id是例如線的id我有3條線 行1從端口A進入B到C到D 第2行從端口A到C到D 第3行從端口A到Z到B

我需要SQL查詢如果我要搜索從端口A到端口的線路C直接或通過其他端口

所以SQL應該說從string_ports中選擇字符串,其中端口C port_order> port A端口順序和它們在同一個字符串上

我該如何編寫SQl?

回答

0

這給一試:

select a.`string_id` 
from `string_ports` as a 
inner join `string_ports` as c 
on a.`string_id` = c.`string_id` 
and a.`port_id` = 1 --whatever port_id A is 
and c.`port_id` = 3 --whatever port_id C is 
and a.`port_order` < c.`port_order`