我有以下数据库
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的数据,所以它会使结果加倍。
我想让此查询只对某个方向在特定日期正确,因为它在数据库中也会显示运行的日期
答案 0 :(得分:0)
如果我正确理解您的问题,要回答该部分只返回一个方向,请尝试以下查询:
SELECT d1.*
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'
AND d1.distance < d2.distance
答案 1 :(得分:-1)
而不是太阳......坐着你应该使用1..7并使值boolean或tinyint
SELECT distinct d1.train_no
FROM train_schedule d1 WHERE DAYOFWEEK(#yyyy-dd-mm#) = 1
INNER JOIN train_schedule d2 ON d2.train_no=d1.train_no
WHERE d1.stn_code = 'JU' and d2.stn_code = 'JP'
这应该有效