Mysql 使用url参数按sql查询排序
我下面有一个巨大而丑陋的查询,我想在目录视图中按它排序。像这样想。如果您有任何意见或回答,我们将提前向您表示万分感谢Mysql 使用url参数按sql查询排序,mysql,ruby-on-rails,ruby,ruby-1.8.7,Mysql,Ruby On Rails,Ruby,Ruby 1.8.7,我下面有一个巨大而丑陋的查询,我想在目录视图中按它排序。像这样想。如果您有任何意见或回答,我们将提前向您表示万分感谢 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
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
选择100-round((当前价格/项目.估计价格)*100)作为百分比,item.cached\u缩略图\u url,item.item\u id,it.name,
ci.当前价格,ci.关闭日期
从项目
在ci.item\u id=item.item\u id上加入目录\u项ci
打开时连接项目(it.item\u id=item.item\u id)
其中(100轮((当前价格/项目预估价格)*100))>49和
item.estimated_price>0和ci.current_price>0和ci.close_date>now()以及item.active=1和ci.active=1和
(当前价格/估计价格)<1
订单依据(ci.close_date
不确定这与ROR有何关系,但无论如何:
100-轮((当前价格/项目.估计价格)*100)作为百分比
,但无论如何在WHERE条件中使用相同的表达式项目.预估价格能否小于零?如果不是,则item.estimated\u price>0
条件过高,如果为零,则(100轮((当前\u price/item.estimated\u price)*100))>49
条件将为假
(当前价格/估计价格)<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
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
选择(100轮((当前价格/项目.预估价格)*100))作为百分比,
item.cached\u缩略图\u url、item.item\u id、it.name、,
ci.当前价格,ci.关闭日期
从项目
在ci.item\u id=item.item\u id上加入目录\u项ci
打开时连接项目(it.item\u id=item.item\u id)
哪里
百分比>49
和ci。当前价格>0
和ci.close_date>now()
和item.active=1
和ci.active=1
订购人
(ci.close_date
这并不能大大改善这种情况,但如果不了解有关DB体系结构的更多原因,我就不能再多说了
顺便说一句,你问题中的链接不起作用(显然是你的本地链接)基于二进制代码的答案,你可以尝试用ARel包装你的查询,然后在你的控制器操作中,你可以在参数散列中传递的字段上排序,如下所示:
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
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
是否使用此查询返回的信息构建目录页?或者别的什么?请不要建议在控制器中放置这样难看的查询(实际上是任何SQL查询)。它们应该放在模型中。同意,这只是一个概述基本思想的肮脏示例,像这样的长查询不属于控制器。