我在显示数据库中的数据时遇到问题。我有jos_stock
带字段的表rem_balance
。rem_balance
有价值0, 126, 12, 9
。在我的网站上。rem_balance
显示它取决于周,即 2012 年第 26 周,余额的值为 0。这意味着 2012 年第 25 周 = 126、2012 年第 24 周 = 12 和 2012 年第 23 周 = 9。
我要做的就是如果当前周的值是 26 等于 0。它将显示之前的余额 126。我该怎么做?有什么建议吗?我正在使用 Ruby on Rails。
这是我在模型中的查询:
SELECT("jos_product.id, jos_product.product_code AS code, jos_product.name,
pc.id AS category_id, pc.name AS category_name, thumbnail, location,
original_image,CONCAT(sm.year, '/', sm.week) as week_start,
CONCAT(sm2.year, '/', sm2.week) AS reprint_week, pr.quantity AS reprint_qty,
jos_stock.rem_balance AS balance")
.joins("INNER JOIN jos_product_category AS pc ON pc.id = jos_product.product_category")
.joins("INNER JOIN jos_stock_movement AS sm ON sm.id = jos_product.start_week")
.joins("LEFT OUTER JOIN jos_stock ON jos_stock.product_id = jos_product.id")
.joins("LEFT OUTER JOIN (select product, max(stock_movement) AS reprint, quantity from
jos_product_reprint group by product) AS pr ON pr.product IN (jos_product.id)")
.joins("LEFT OUTER JOIN jos_stock_movement AS sm2 ON sm2.id = pr.reprint")
.where("jos_product.published = 1 #{ search_query }")
.order("jos_product.product_code ASC")
.group("jos_product.product_code")