获取与两个表相互链接的一列的值

时间:2015-05-24 11:29:32

标签: mysql sql

当我找到停止名称时,我想尝试获取以下内容我想要找到'路线' (Promise.coroutine(function*(){ // generators ftw if(nodir) yield fs.mkdirAsync("somedirectory"); var data = yield fs.readFileAsync(path.dirname(__dirname) + '/util/sample_config.yml'); log("pulled sample content"); yield fs.writeFileAsync("db/config.yml", data); log('successfully wrote file to %s/db/config.yml', process.cwd()); })(); process.on("unhandledRejection", function(p, r){ throw r; // quit process on promise failing }); 表中的{'方向'如果可能的话)基于routes表格中的arrivaltimeweekday

路线表

arrivaltimes

停止表格

CREATE TABLE IF NOT EXISTS routes (
route_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
direction VARCHAR(30) NOT NULL, 
route INT(11) NOT NULL )

到达时间表

CREATE TABLE IF NOT EXISTS stops
stop_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
name varchar(30) NOT NULL, 
lat double(10,6) NOT NULL, 
longi double(10,6)NOT NULL)

我的查询

CREATE TABLE IF NOT EXISTS arrivaltimes(
    arrivaltimes_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
     weekday VARCHAR(20) NOT NULL,
    route INT(11) NOT NULL, 
    arrivaltime time NOT NULL,
    stop_id INT, FOREIGN KEY fk_stop_id(stop_id) REFERENCES stops(stop_id), 
    route_id INT, FOREIGN KEY fk_route_id(route_id) REFERENCES routes(route_id) )

如何解决?

1 个答案:

答案 0 :(得分:2)

加入所有3个表

SELECT r.route
FROM routes AS r
JOIN arrivaltimes AS a ON a.route_id = r.route_id
JOIN stops as s on s.stop_id = a.stop_id
WHERE a.weekday = 'sat'
AND time_format(a.arrivaltime,'%H:%i') = time_format(curtime() ,'%H:%i')
AND s.name = 'Aldgate'