我对我的交互器有这个查询:
Variant.where(code: context.codes)
.joins("LEFT OUTER JOIN prices p
ON p.variant_id = variants.id")
.select("code,
actual_price,
selling_price,
p.cost")
返回:
#<Interactor::Context codes=["PRDCTA-V1", "PRDCTA-V2"], body=[#<Variant id: nil, selling_price: nil, code: "PRDCTA-V2", actual_price: nil>,
#<Variant id: nil, selling_price: #<BigDecimal:7ff1d13b9118,'0.1E3',9(18)>, code: "PRDCTA-V1", actual_price: nil>]
我不知道为什么 prices
表中的 p.cost
没有出现,这意味着我的选择是错误的。我试过改成cost
,prices.cost
还是不行。
我的预期输出应该是
[#<Variant id: nil, selling_price: nil, code: "PRDCTA-V2", actual_price: nil, cost:{value here}>,
最佳答案
试试这个,在选择中为成本分配一个别名
p.cost AS price_cost
然后,通过variants.first.price_cost
获取它
或者,只需从 attributes
variants.first.attributes['cost']
会返还费用。
希望对您有所帮助!
关于sql - 使用 select 从不同的表中获取列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57637986/