2015-05-02 104 views
1

我很努力地在rails中转换sql查询。在给定sql查询的情况下转换复杂查询

背景

我有一个名为总线3个表,停止和调度。总线表具有字段ID和名称。停止表具有字段ID和名称。计划表包含字段标识,bus_id,stop_id,到达和bustag。

这是查询我在SQL

select A.bus_id as busid, A.stop_id as source, A.arrival as atime, B.arrival as dtime from 
(SELECT * from schedules as S where S.stop_id = #{startStopId}) A 
inner join 
(SELECT * from schedules as S where S.stop_id = #{endStopId}) B 
on A.bustag = B.bustag 
where A.arrival < B.arrival 
and A.arrival > CURTIME(); 

在铁轨我已经做到了这一点至今

@possible_buses = Schedule.where(stop_id: [startStopId,endStopId]) 

现在我想做的事情一样MySQL查询这是我想进一步处理这个可能的公共汽车列表获得startStop中的公交车列表,其尾部标志等于公共汽车的尾部标志。停止处,起始处的到达时间少于终止处的到达时间。

如果有人能帮助我,我将非常感激。我在轨道查询方面不太好,这对我有很大的帮助。

示例表

BusTable    StopTable  ScheduleTable 
id Name    id Name   id bus_id stop_id arrival bustag 
1 ttc(inbound)  1 mall   1 1  1  3:00  1 
2 ttc(outbound)  2 home   2 1  2  3:15  1 
         3 downtown  3 1  3  3:30  1 
         4 uptown  4 1  4  3:45  1 
             5 1  1  3:15  2 
             6 1  2  3:30  2 
             7 1  3  3:45  2 
             8 1  4  4:00  2 
             9 2  4  2:55  3 
             10 2  3  3:10  3 
             11 2  2  3:25  3 
             12 2  1  3:35  3 
             13 2  4  3:10  4 
             14 2  3  3:20  4 
             15 2  2  3:30  4 
             16 2  1  3:45  4 

示例查询和预期产出

例如,如果用户想从商场走在2:30到住宅区然后将下面的ID应该从返回时间表: - > 1,5。由于这两个Ids都会为您提供bus_id,可以让您从商场到上城。

我希望这个更清楚。请随时索取更多信息。谢谢。

+0

也许我们应该从简化查询 – Strawberry

+1

开始,如果你用表格数据样本和展开结果和/或sqlfiddle来说明你的帖子,那么这将非常有帮助 – Alex

+0

我可以这样做,给我一分钟。谢谢 – Unknowntiou

回答

0

其他awnsers提供了正确的SQL轨自定义查询。所以我只展示如何在Rails中执行原始SQL。

Rails不仅支持活动记录,还支持执行原始SQL。它返回一个数组,数组中的每个元素都是一个散列,其中所有选定的列作为键和数据作为值。返回的数组就像活动记录方式返回的那样。

这里有一个例子:

# first establish connection, if not explicitly specify establish connection, it should use default configuration, which is config/database.yml 
ActiveRecord::Base.establish_connection(
    :adapter => "mysql2", 
    :host => "localhost", 
    :port => 3306, 
    :username => "myuser", 
    :password => "mypass", 
    :database => "somedatabase", 
    :pool => 1, 
    :timeout => 5000 
) 
sql = "select name, age from users where age < 30" 
raw = ActiveRecord::Base.connection.execute(sql) 
# raw is like [{"name" => "Tom", "age" => 28}, {"name" => "Bob", "age" => 26}] 
raw.each(:as => :hash) do |row| 
    puts row.inspect # row is hash, something like {"name" => "Tom", "age" => 28} 
end 

您可以运行rails runner 'puts ActiveRecord::Base.configurations.inspect'检查您的默认数据库连接信息。

+0

您是否需要指定连接名称主机名和用户名? – Unknowntiou

+0

@Unknowntiou答案已更新。 – coderz

0

这是一个更好的查询,理论上应该做同样的事情。

我会在更多细节提供后更新我的回复。

SELECT 
    start.bus_id busid, 
    start.stop_id source, 
    start.arrival atime, 
    stop.arrival dtime 
FROM 
    schedules start 
INNER JOIN 
    schedules stop ON 
     stop.bustag = start.bustag 
     AND stop.arrival > start.arrival 
WHERE 
    start.stop_id = #{startStopId} 
    AND stop.stop_id = #{endStopId}) 
    AND start.arrival > now(); 
+0

嗨Augwa我刚刚添加更多的信息,我想知道如果你查询是可以做在Rails使用活动记录查询方法 – Unknowntiou

+0

它应该是可能的,因为这个查询没有什么疯狂的事情发生。我不熟练使用活动记录或轨道,所以我不能给你一个超越sql查询方面的适当响应。 – Augwa

0

对不起,应该是一个评论,但只要我做的工作对你

http://sqlfiddle.com/#!9/45f47/9

而且我希望你能解释什么是错的查询或结果我张贴此回答未来的真实回应。

因此,您可以在我的小提琴中看到您的查询(选择A.id字段进行小的更改)已经返回您预期的1,5值。那么,什么是错的?你在找什么其他结果?

select A.id, A.bus_id as busid, A.stop_id as source, A.arrival as atime, B.arrival as dtime from 
(SELECT * from ScheduleTable as S where S.stop_id = 1) A 
inner join 
(SELECT * from ScheduleTable as S where S.stop_id = 4) B 
on A.bustag = B.bustag 
where A.arrival < B.arrival 
and A.arrival > '2:30'; 

编辑https://stackoverflow.com/a/15408419/4421474这里是解决如何运行与

+0

我想在Rails中做到这一点,而不使用sql – Unknowntiou