如果sql查询计数小于

时间:2012-07-31 04:10:45

标签: mysql sql

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

所以问题是,我可以检查(计算?)整个查询内部的结果,并将该17小时参数打开到更大的数字以返回至少12个结果吗? < / p>

提前致谢。

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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`

答案 2 :(得分:0)

我有一位资深同事伸出援助之手,我们采取了不同的方式。我花了小时间隔并限制了sql查询,并通过ruby中的结果循环返回&gt; 12个结果,每次增加间隔+17小时,直到我们得到+ 12.一旦我们达到那个阈值,我们就会破裂。谢谢你的帮助!

 #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