我有以下数据库
表的表结构trains
CREATE TABLE IF NOT EXISTS `trains` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`train_no` varchar(5) COLLATE latin1_general_ci DEFAULT NULL,
`train_name` varchar(50) COLLATE latin1_general_ci NOT NULL,
`runsfrom` varchar(50) COLLATE latin1_general_ci NOT NULL,
`SUN` varchar(3) COLLATE latin1_general_ci NOT NULL,
`MON` varchar(3) COLLATE latin1_general_ci NOT NULL,
`TUE` varchar(3) COLLATE latin1_general_ci NOT NULL,
`WED` varchar(3) COLLATE latin1_general_ci NOT NULL,
`THU` varchar(3) COLLATE latin1_general_ci NOT NULL,
`FRI` varchar(3) COLLATE latin1_general_ci NOT NULL,
`SAT` varchar(3) COLLATE latin1_general_ci NOT NULL,
`DOE` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1912 ;
有这样的数据:
INSERT INTO `trains` VALUES (269, '12307', 'HWH JU EXPRESS', 'HOWRAH JN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN', '2013-03-24');
INSERT INTO `trains` VALUES (270, '12308', 'JU HWH SUPFAST', 'JODHPUR JN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN', '2013-03-24');
INSERT INTO `trains` VALUES (381, '12461', 'MANDOR EXPRESS', 'DELHI', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN', '2013-03-24');
INSERT INTO `trains` VALUES (382, '12462', 'MANDOR EXPRESS', 'JODHPUR JN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN', '2013-03-24');
表的表结构train_number
CREATE TABLE IF NOT EXISTS `train_number` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`train_no` varchar(5) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1912 ;
有这样的数据:
INSERT INTO `train_number` VALUES (269, '12307');
INSERT INTO `train_number` VALUES (270, '12308');
INSERT INTO `train_number` VALUES (381, '12461');
表的表结构train_schedule
CREATE TABLE IF NOT EXISTS `train_schedule` (
`train_no` varchar(5) NOT NULL,
`stn_code` varchar(20) NOT NULL,
`stn_name` varchar(50) NOT NULL,
`route_no` varchar(2) NOT NULL,
`arr_time` varchar(5) NOT NULL,
`dep_time` varchar(5) NOT NULL,
`halt_time` varchar(5) NOT NULL,
`distance` varchar(4) NOT NULL,
`day` varchar(2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
有这样的数据:
INSERT INTO `train_schedule` VALUES ('12307', 'HWH ', 'HOWRAH JN ', '1', 'Sourc', '23:30', '', '0', '1');
INSERT INTO `train_schedule` VALUES ('12307', 'BWN ', 'BARDDHAMAN JN ', '1', '00:35', '00:37', '2:00', '95', '2');
INSERT INTO `train_schedule` VALUES ('12307', 'ASN ', 'ASANSOL JN ', '1', '01:52', '01:56', '4:00', '200', '2');
INSERT INTO `train_schedule` VALUES ('12307', 'DHN ', 'DHANBAD JN ', '1', '03:05', '03:15', '10:00', '259', '2');
现在我想找到在特定日期找到两个车站之间的火车。所以我尝试了这个查询
SELECT distinct d1.train_no
FROM train_schedule d1
INNER JOIN train_schedule d2 ON d2.train_no=d1.train_no
WHERE d1.stn_code = 'JU' and d2.stn_code = 'JP'
但它同时显示了从 JU 到 JP 以及从 JP 到 JU 的数据,因此它使结果加倍。
我想让这个查询只 在特定日期的一个方向上正确,因为它运行的天数也在数据库中给出