2013-02-13 53 views
1

我正在使用HTML選擇輸入,並啓用了多個選擇,我正在查看所選選項並嘗試處理我的SQL ...但是,我從SQL中收到一條警告,告訴我如果我的select語句是正確的,我應該使用「SET SQL_BIG_SELECTS = 1」,因爲JOIN超過了限制?SQL_BIG_SELECTS,提煉我的查詢

這裏是我的查詢:

" 
     SELECT wposts.*, wpostmeta.* 
     FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta, $wpdb->postmeta wpostmeta2, $wpdb->postmeta wpostmeta3 
     WHERE wposts.ID = wpostmeta.post_id 
      AND wposts.ID = wpostmeta2.post_id 
      AND wposts.ID = wpostmeta3.post_id 
      AND wpostmeta.meta_key = 'listing_subtype' 
       AND wpostmeta.meta_value = '$search_home_type' 
      AND wpostmeta2.meta_key = 'map_area' 
       AND "; 

       $count = 0; 
       foreach ($params['search_map_area'] as $map_area) : 
        if ($count != 0) : 
         $querystr .= "OR "; 
        endif; 
        $querystr .= "wpostmeta2.meta_value = '$map_area' "; 
        $count++; 
       endforeach; 

    $querystr .= "AND wpostmeta3.meta_key = 'price_current' 
       AND wpostmeta3.meta_value BETWEEN $search_price_min AND $search_price_max 
      AND wposts.post_status = 'publish' 
      AND wposts.post_type = 'vreb_property' 
     ORDER BY wposts.post_date DESC 
     LIMIT 0, 20 
     "; 

有沒有更好的方式來查詢呢?這是多次OR wpostmeta2.meta_value = '$map_area',似乎導致我的腳本超時......

+0

如果你的選擇是那麼大,我會擔心生成的查詢真的很慢。代碼運行後,查詢看起來像什麼? – Cfreak 2013-02-13 19:45:31

+0

它實際上不運行...它現在超時。即使使用「SET SQL_BIG_SELECTS = 1」 – dcolumbus 2013-02-13 19:46:08

回答

0
FROM $wpdb->posts wposts, 
     INNER JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id 
     INNER JOIN $wpdb->postmeta wpostmeta2 ON wposts.ID = wpostmeta2.post_id 
     INNER JOIN $wpdb->postmeta wpostmeta3 ON wposts.ID = wpostmeta3.post_id 
    WHERE ... 

使用JOIN ... ON可能是優於其所有的ID關係在WHERE類。


你真的應該使用參數化查詢,所以你不打開自己SQL注入,因爲它似乎並不像你正確轉義任何您使用的字符串。 http://www.php.net/manual/en/mysqli.real-escape-string.php

但是,如果你在一個私人/內部應用程序和安全工作被詛咒......

  $temp = "'" . implode("','", $params['search_map_area']) . "'"; 
      if($temp != "''") 
       $querystr .= "wpostmeta2.meta_value IN ($temp)"; 
      else 
       $querystr .= " 1=1 "; 

使用IN關鍵字來代替多個「或COL =‘VAL’」也首選。