我的火车详情表,
Table Name- traindetailtb
Id Train_Number Train_Name From To
1 27658 Venad Express ABC XYZ
2 27659 Venad Express XYZ ABC
和我的火车日表,
Table Name- traindaystb
Id Train_Number Days
1 27658 Sunday
2 27658 Wednesday
3 27659 Saturday
4 27659 Friday
这是我的列车时刻表,
Table Name- scheduletb
Id Train_Number Station Time
1 27658 ABC 09:00am
2 27658 CDE 10:00am
3 27658 XYZ 11:00am
4 27659 XYZ 12:00pm
5 27659 CDE 01:00pm
6 27659 ABC 02:00pm
将有两个输入,From和To
我需要一个查询,在给定的电台之间提供所有列车(即Train_Number,Train_Name,Days)
例如: 对于输入From-CDE To-XYZ ----> 输出将是 - 27658 Venad Express S W
和输入From-XYZ To-CDE ----> 输出将是 - 27659 Venad Express M T
任何人都可以帮我这样做。
答案 0 :(得分:2)
你在找这样的东西吗?
SELECT q.train_number, MAX(t.train_name) train_name, GROUP_CONCAT(LEFT(d.days, 1)) days
FROM
(
SELECT s.train_number
FROM scheduletb s JOIN scheduletb e
ON s.train_number = e.train_number
WHERE s.station = 'CDE' -- from
AND e.station = 'XYZ' -- to
AND s.id < e.id
) q JOIN traindaystb d
ON q.train_number = d.train_number JOIN traindetailtb t
ON q.train_number = t.train_number
GROUP BY q.train_number
CDE -> XYZ
的输出:
| TRAIN_NUMBER | TRAIN_NAME | DAYS | |--------------|---------------|------| | 27658 | Venad Express | S,W |
XYZ -> CDE
的输出:
| TRAIN_NUMBER | TRAIN_NAME | DAYS | |--------------|---------------|------| | 27659 | Venad Express | S,F |
这是 SQLFiddle 演示
答案 1 :(得分:0)
试试这个
SELECT t.Train_Number,t.Train_Name,d.Days
from traindetailtb t,traindaystb d
WHERE
t.Train_Number=d.Train_Number AND From='ABC' AND To='XYZ';
然后阅读输入并将其放入from =&#39; &#39;并且= =&#39; &#39 ;;
这个单一查询将返回两个输出?
答案 2 :(得分:0)
您可以尝试这样的事情: -
SELECT A.Train_number, A.Train_name, C.days
FROM traindetailtb A, traindaystb B, scheduletb C
WHERE C.Train_Number = A.Train_Number
AND A.Train_Number = B.Train_Number
AND FROM = 'ABC' AND TO = 'XYZ';
这可能会对你有帮助。
答案 3 :(得分:0)
我建议如下:
select
traindetailtb.Train_Number,
traindetailtb.Train_Name,
traindetailtb.From,
traindetailtb.To,
tranddaystb.Days,
scheduletb.Time
From
traindetailtb
join trandaystb on
traindetailtb.Train_Number=traindaystb.Train_Number
join scheduletb on
traindetailtb.Train_Number=scheduletb.Train_Number and
traindetailtb.From=scheduletb.From
where
traindetailtb.From=$inputFrom
and
traindetailtb.To=$inputTo
答案 4 :(得分:0)
使用自联接,您可以执行此操作,假设时间实际上是时间字段
SELECT a.Train_Number, d.Train_Name, GROUP_CONCAT(e.Days)
FROM scheduletb a
INNER JOIN scheduletb b
ON a.Train_Number = b.Train_Number
AND b.Time > a.Time
INNER JOIN traindetailtb d
ON a.Train_Number = d.Train_Number
INNER JOIN traindaystb e
ON a.Train_Number = e.Train_Number
WHERE a.Station = 'CDE'
AND b.Station = 'XYZ'
GROUP BY a.Train_Number, d.Train_Name
假设您有时间的实时字段
如果您只想要从CDE直接到没有中间站的XYZ的列车,您可以在表中添加LEFT OUTER JOIN,寻找时间在另外2之间的站点: -
SELECT a.Train_Number, d.Train_Name, GROUP_CONCAT(e.Days)
FROM scheduletb a
INNER JOIN scheduletb b
ON a.Train_Number = b.Train_Number
AND b.Time > a.Time
LEFT OUTER JOIN scheduletb c
ON a.Train_Number = c.Train_Number
AND c.Time > a.Time
AND c.Time < b.Time
INNER JOIN traindetailtb d
ON a.Train_Number = d.Train_Number
INNER JOIN traindaystb e
ON a.Train_Number = e.Train_Number
WHERE a.Station = 'CDE'
AND b.Station = 'XYZ'
AND c.Station IS NULL
GROUP BY a.Train_Number, d.Train_Name
答案 5 :(得分:-1)
如果要将单个单元格中的天数可视化而不是分散成多行,请使用GROUP_CONCAT
作为聚合函数。
select tde.train_number, tde.train_name, GROUP_CONCAT(tda.days, ",") as days
from traindetailtb tde
join traindaystb tda on tda.train_number=tde.train_number
where tde.from = $user_input_from
and tde.to = $user_input_to
group by tde.train_number, tde.train_name