我有一个遗留数据库,我需要将其转换为新数据库以用于 Rails 6 应用程序。遗留数据库有 21 列和 900,000 多行。我只对根据两列的组合提取某些没有重复项的列数据感兴趣。我有名为 depth
、name
、product_number
和 country
的列。我想提取 name
和 product_number
,但只提取基于 product_number
和 country
组合的唯一行其中 depth
为 1。例如,如果我使用 product_number 11122000 和 country AUS,我有 115 行,但我只想要其中的一行。我想将结果保存到一个变量中,因为我必须将新数据库中的列命名为不同于旧数据库中的列。我已经为其他遗留数据库转换编写了 rake 任务,但我不知道如何提取这些列。我试过这个:
Database.where(depth: 1).group(:product_number, country)
这为我提供了包含产品编号和国家/地区的数组散列。如何从此哈希中提取名称和产品编号?或者是否有更好的方法来拉取所需的行?
最佳答案
问题在于,在关系数据库理论中,如果您group by
unique product_number
和country
可能会有多个结果,它们可能name
有不同的值。所以 SQL 不知道如何为 name
值选择哪一行。
您可能必须在 ruby 中处理它。
Database.where(depth: 1).group(:product_number, :country).map |db|
Database.where(depth: 1, product_number: db.product_number, country: db.country).first
end
关于mysql - 将旧版 v1 数据迁移到 Rails 6 应用程序中的 v2 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58755705/