Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在给定sql查询的rails中转换复杂查询_Mysql_Sql_Ruby On Rails_Ruby - Fatal编程技术网

Mysql 在给定sql查询的rails中转换复杂查询

Mysql 在给定sql查询的rails中转换复杂查询,mysql,sql,ruby-on-rails,ruby,Mysql,Sql,Ruby On Rails,Ruby,我正在努力转换rails中的sql查询 背景 我有三张表,分别是巴士、车站和时刻表。总线表具有字段id和名称。停止表具有字段id和名称。时间表表中有字段id、公共汽车id、车站id、到达和布斯塔格 这是我在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 =

我正在努力转换rails中的sql查询

背景

我有三张表,分别是巴士、车站和时刻表。总线表具有字段id和名称。停止表具有字段id和名称。时间表表中有字段id、公共汽车id、车站id、到达和布斯塔格

这是我在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();
现在我想做和mysql查询相同的事情,我想进一步处理这个可能的总线列表,得到startStop的总线列表,bustag等于endStop的总线bustag,其中startStop的到达时间小于endStop的到达时间

如果有人能帮我,我将不胜感激。我不擅长rails查询,这对我有很大帮助

样本表

示例查询和预期输出

例如,如果用户希望在2:30从购物中心前往住宅区,则应从时间表返回以下ID:->1,5。因为这两个身份证都会给你提供从购物中心到住宅区的巴士身份证


我希望这更清楚。请随时询问更多的信息。谢谢。

这里有一个更好的查询,理论上也应该这样做

一旦提供更多详细信息,我将更新我的回复

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();

对不起,这应该是一个评论,但一旦我为你做了工作

我希望你能解释一下你的问题或结果有什么不对,我会把这个答案贴出来,以备将来真正的回复

因此,正如您在我的fiddle中所看到的,对select 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';

编辑这里是如何使用rails运行自定义查询的解决方案

其他用户提供了正确的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”来检查默认的数据库连接信息。

也许我们应该从简化查询开始。如果您用表数据示例和expacted result和/或SqlFiddle来说明您的帖子,那将非常有帮助。我可以做到这一点,请稍候。谢谢,我已经添加了更多细节@Alex。如果您不清楚,并且预期结果是…?,请询问更多问题???嗨,Augwa,我刚刚添加了更多信息,我还想知道您是否可以在Rails中使用活动记录查询方法进行查询。这应该是可能的,因为这个查询没有什么疯狂的事情发生。我对active record或rails不太熟悉,因此除了sql查询之外,我无法给您提供正确的响应。我希望在rails中不使用sql来完成此操作。您是否需要指定连接名主机名和用户名?@unknown您的答案已更新。
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';
# 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