2011-08-26 55 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`