船只跟踪数据库架构

时间:2011-08-26 00:56:44

标签: mysql sql database

我有一个船只跟踪数据库架构,其中包含下表中的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

如果我想直接搜索从端口A到端口C的路径或者通过其他端口传输,我需要一个SQL查询

所以SQL应该说string_ports中的select字符串,其中是端口C port_order> port端口顺序,它们位于同一个字符串

我怎么写那个SQl ??

1 个答案:

答案 0 :(得分: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`