2012-07-26 32 views
0

我在下面有這個巨大的醜陋查詢,我想在目錄視圖中對它進行排序。思考類似http://wow.dev:3000/catalog_items?&order=deals。百萬提前感謝您的任何意見或答案。使用URL參數進行sql查詢排序

select 100 - round((current_price/item.estimated_price)*100) as percent, item.cached_thumbnail_url, item.item_id, it.name, 
      ci.current_price, ci.close_date 
      from item 
      join catalog_item ci on ci.item_id = item.item_id 
      join item_translations as it on (it.item_id = item.item_id) 
      where (100 - round((current_price/item.estimated_price)*100)) > 49 and 
      item.estimated_price > 0 and ci.current_price > 0 and ci.close_date > now() and item.active = 1 and ci.active = 1 and 
      (current_price/estimated_price) < 1 
      order by (ci.close_date < DATE_ADD(now(), INTERVAL 17 hour)) and (item.estimated_price - current_price) desc 
      limit 12 
+0

是您使用此查詢返回的信息構建目錄頁面?或者是其他東西? – octern 2012-07-26 05:46:37

回答

0

不知道如何可能與ROR,但不管怎麼說:

  1. 您在SELECT子句中有100 - round((current_price/item.estimated_price)*100) as percent,但無論如何使用在有條件同樣的表情。

  2. 可以item.estimated_price小於零?如果不是item.estimated_price > 0條件是過度的,如果它是零(100 - round((current_price/item.estimated_price)*100)) > 49條件會出現假

  3. (current_price/estimated_price) < 1過多的原因相同

所以,你的查詢可以更清楚地改寫有點像這樣:

select (100 - round((current_price/item.estimated_price)*100)) as percent, 
    item.cached_thumbnail_url, item.item_id, it.name, 
    ci.current_price, ci.close_date 
from item 
    join catalog_item ci on ci.item_id = item.item_id 
    join item_translations as it on (it.item_id = item.item_id) 
where 
    percent > 49 
    and ci.current_price > 0 
    and ci.close_date > now() 
    and item.active = 1 
    and ci.active = 1 
order by 
    (ci.close_date < DATE_ADD(now(), INTERVAL 17 hour)) 
    and (item.estimated_price - current_price) desc 
limit 12 

這並沒有改善這種情況,但是我不能再多說一些關於數據庫架構的更多理由。

BTW你的問題的鏈接無法正常工作(這是你明明本地鏈路)

0

上binarycode的答案構建,你可以嘗試包裹在AREL您的查詢,然後在你的控制器動作,您想訂購在params哈希表傳遞領域,像這樣:

class ItemsController < ApplicationController 
    ... 
    def index 
    @items = Item.select(%{(100 - round((current_price/item.estimated_price)*100)) as percent, 
       item.cached_thumbnail_url, item.item_id, it.name, 
       ci.current_price, ci.close_date}). 
      joins('join catalog_item ci on ci.item_id = item.item_id'). 
      joins('join item_translations as it on (it.item_id = item.item_id)'). 
      where('percent > 49 and ci.current_price > 0 and ci.close_date > now() and item.active = 1 and ci.active = 1'). 
      order(%{(ci.close_date < DATE_ADD(now(), INTERVAL 17 hour)) 
       and (item.estimated_price - current_price) desc)}).limit(12) 
    @items = @items.order(params[:order]) if params[:order] 
    ... 
end 

編輯

由於binarycode低於所指出的,你可能想通過移動,使控制器代碼吸塵器動作外主查詢,大概在進入該Item模型的方法,並確保你仍然從它返回一個Relation對象(如當前語句一樣),所以你可以鏈上的額外order後:

def index 
    @items = Item.by_price_ratio 
    @items = @items.order(params[:order]) if params[:order] 
end 
+1

請不要將這樣醜陋的查詢(實際上是任何SQL查詢)放在控制器中。他們應該被放置在模型中。 – binarycode 2012-07-26 07:03:04

+0

同意,這只是一個骯髒的例子來概述基本思想,像這樣的長時間查詢不屬於控制器。 – HargrimmTheBleak 2012-07-26 07:16:18