【问题标题】:Rails use multiple query in single query for importing csv file dataRails 在单个查询中使用多个查询来导入 csv 文件数据
【发布时间】:2020-08-18 07:39:56
【问题描述】:

如何在 IN 运算符查询中传递 araay 数据。

data = ["055226", "000879", "069889", "078720", "078764"] //use array data in query statements IN operator

file_data = ActiveRecord::Base.connection.execute("SELECT t1.user_id, t2.dept_id, t1.price, 
t3.first_name, t3.last_name
      FROM price_table t1
      INNER JOIN user_branch_table t2
        on t1.user_id = t2.user_id 
      INNER JOIN user_deatils_table t3
        on t1.user_id = t3.user_id
      where t1.user_id in (#{data}) and now() between t2.start_dt and t2.end_dt").first

我尝试了不同的方法,但没有得到正确的。

【问题讨论】:

    标签: mysql sql ruby-on-rails


    【解决方案1】:

    以下应该可以工作:

        file_data = ActiveRecord::Base.connection.execute("SELECT t1.user_id, t2.dept_id, t1.price, 
    t3.first_name, t3.last_name
          FROM price_table t1
          INNER JOIN user_branch_table t2
            on t1.user_id = t2.user_id 
          INNER JOIN user_deatils_table t3
            on t1.user_id = t3.user_id
          where t1.user_id in (#{data.join(',')}) and now() between t2.start_dt and t2.end_dt").first
    

    上面的不适用于字符串输入,下面的应该适用于字符串输入

        ActiveRecord::Base.connection.execute(
          ActiveRecord::Base.send(:sanitize_sql_array, ["SELECT 
            t1.user_id, t2.dept_id, t1.price, 
            t3.first_name, t3.last_name
            FROM price_table t1
            INNER JOIN user_branch_table t2
            on t1.user_id = t2.user_id 
            INNER JOIN user_deatils_table t3
            on t1.user_id = t3.user_id
            where t1.user_id in (?) and now() between t2.start_dt and t2.end_dt", data])).first
    

    【讨论】:

    • 感谢您的回复,第二个正在工作,但我需要在屏幕“file_data”上显示时添加或附加文件数据的新价格。我在这方面遇到了问题。
    • 现在,我正在使用 file_data = ActiveRecord::Base.connection.execute( ActiveRecord::Base.send(:sanitize_sql_array, ["SELECT t1.user_id, t2.dept_id, t1.price, t3.first_name, t3.last_name FROM price_table t1 INNER JOIN user_branch_table t2 on t1.user_id = t2.user_id INNER JOIN user_deatils_table t3 on t1.user_id = t3.user_id where t1.user_id in (?) and now() between t2.start_dt and t2.end_dt", data])) 从 db 获取数据,但我还有另一个数据要添加到每个记录数据末尾的文件数据中。 file_new_price{"new_price"=>"900", new_price"=>"960"} 这个怎么附加?
    • 好的,我从上面的记录中得到 file_data = [{"user_id"=>"101", "dept_id"=>"01", "price"=>"950", "first_name"=>"ajit", "last_name"=>"shinde"}, {"user_id"=>"102", "dept_id"=>"02", "price"=>"950", "first_name"=>"vikas", "last_name"=>"patil"}, {"user_id"=>"103", "dept_id"=>"03", "price"=>"950", "first_name"=>"rohit", "last_name"=>"patel"}] 这是数据库记录,我想在 csv_data = [{"user_id"=>"101", "date"=>"2020-08-13", "new_price"=>"1000"}, {"user_id"=>"102", "date"=>"2020-08-13", "new_price"=>"800"}] 的数据末尾添加 new_price 每个记录
    • 在查询中 .first 在第二个查询中不起作用。只给了一个记录。我试图获取最近记录的每个用户。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 2021-08-20
    • 2014-02-11
    • 2016-09-06
    相关资源
    最近更新 更多