1

如果查询不符合计数阈值,您可以计算查询的结果并使查询的一部分动态化吗?也许使用案例?正如你在下面看到的,我有一个很大的愚蠢的 sql 查询,它有很多事情发生(忽略日期调整部分,抱歉)。有时它从超过 10k 个项目的数据库中返回少于 12 个结果,因为它需要在关闭后 17 小时内的项目。

所以问题是,我是否可以检查(计数?)整个查询的结果,并将 17 小时参数开放为更大的数字以返回至少 12 个结果?

提前致谢。

SELECT 100 - round((current_price / item.estimated_price)*100) as percent, item.cached_thumbnail_url, item.item_id, current_price, close_date, catalog_item_id FROM catalog_item AS ci JOIN item on item.item_id = ci.item_id
 JOIN item_translations it ON (it.item_id = item.item_id) WHERE ((100 - round((current_price / item.estimated_price)*100)) > 49 AND
 item.estimated_price > 0 AND
 current_price > 0 AND
 ci.close_date > DATE_ADD(NOW(), interval 14400 second) AND
 item.active = 1 AND
 ci.active = 1 AND
 (current_price / estimated_price) < 1 AND 
 (ci.close_date < DATE_ADD(DATE_ADD(NOW(), interval 14400 second), INTERVAL 17 HOUR))) ORDER BY (item.estimated_price - current_price) DESC LIMIT 12
4

3 回答 3

0

不确定,但也许你可以尝试这样的事情或使用子查询:

SET @cnt = 0;

SELECT ...
FROM catalog_item AS ci
    JOIN item on item.item_id = ci.item_id
    JOIN item_translations it ON (it.item_id = item.item_id)
WHERE ((100 - round((current_price / item.estimated_price)*100)) > 49 AND
        ...
     (ci.close_date < DATE_ADD(DATE_ADD(NOW(), interval 14400 second), INTERVAL IF(@cnt < 12, 20, 17) HOUR)))
    AND (SELECT @ids := @cnt + 1)
ORDER BY (item.estimated_price - current_price) DESC LIMIT 12
于 2012-07-31T04:21:02.330 回答
0
`SELECT 100 - round((current_price / item.estimated_price)*100) as percent, item.cached_thumbnail_url, item.item_id, current_price, close_date, catalog_item_id FROM catalog_item AS ci JOIN item on item.item_id = ci.item_id
 JOIN item_translations it ON (it.item_id = item.item_id) WHERE ((100 - round((current_price / item.estimated_price)*100)) > 49 AND
 item.estimated_price > 0 AND
 current_price > 0 AND
 ci.close_date > DATE_ADD(NOW(), interval 14400 second) AND
 item.active = 1 AND
 ci.active = 1 AND
 (current_price / estimated_price) < 1 AND
CASE WHEN ( SELECT COUNT(1) FROM catalog_item)>1000 
THEN  (ci.close_date < DATE_ADD(DATE_ADD(NOW(), interval 14400 second), INTERVAL 17 HOUR)))
--and so on 
END  ORDER BY (item.estimated_price - current_price) DESC LIMIT 12`
于 2012-07-31T06:32:45.647 回答
0

I had a senior colleague lend a hand, and we went a different route. I took the hour interval and limit out of the sql query, and the looped through the results in ruby to return > 12 results, each time increasing the interval +17 hours, until we get + 12. Once we hit that threshold we break. Thanks for the help!

 #DEALS TAB BEGIN
  def self.deals
    #calculate the raw percentage, minus that from 100 to show percentage off
    options = {: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, ci.catalog_item_id",

               :from => "catalog_item AS ci",
               :joins => "JOIN item on item.item_id = ci.item_id
                                       JOIN item_translations it ON (it.item_id = item.item_id)",
               :order => "(item.estimated_price - current_price) DESC",

               :conditions => "(100 - round((current_price / item.estimated_price)*100)) > 49 AND
                                  item.estimated_price > 0 AND
                                  ci.current_price > 0 AND
                                  ci.close_date > DATE_ADD(NOW(), interval #{-1*Time.now.utc_offset} second) AND
                                  item.active = 1 AND
                                  ci.active = 1 AND
                                  (current_price / estimated_price) < 1 "}

    #loop adding 17 hours each pass until < 12 results show
    catalog_items = CatalogItem.all(options)
    _deals = []
    if catalog_items.present?
      last_close_date =  CatalogItem.first(:conditions => options[:conditions], :from => options[:from], :joins => options[:joins], :order =>  "ci.close_date DESC", :select => options[:select]).close_date
      start_time = Time.now
      interval = 17.hours
      end_time = start_time + interval
      min_needed_for_view = 12
      until _deals.size >= min_needed_for_view or last_close_date < start_time
        catalog_items.each do |ci|
          if ci.close_date < end_time && ci.close_date > start_time
            _deals.push(ci)
            break if _deals.size >= min_needed_for_view
          end
        end
        start_time = end_time
        end_time += interval
      end
    end
    _deals
  end
于 2012-08-01T03:26:57.737 回答