2012-10-15 67 views
0

我在這裏有一個var_dumped查詢,顯示錯誤的結果。錯誤的MYSQL結果

select p.* 
, case when p.specials_new_products_price >= 0.0000 
    and (p.expires_date > Now() or p.expires_date IS NULL) 
    and p.status != 0 then 
     p.specials_new_products_price 
    else 
     p.products_price 
end price 

from wp_global_products_table p 
INNER JOIN wp_blogs s ON s.blog_id = p.blog_id 
where p.products_name like '%indifi%' 
or p.products_description like '%indifi%' 
and s.countries_id = '168' 
and global_category_id = '15' 
and p.display_product = '1' 
and p.products_status = '1' 
order by p.products_date_added DESC, p.products_name" 

這是原始查詢。

$catglobal_sql = "select p.*, case when p.specials_new_products_price >= 0.0000 and (p.expires_date > Now() or p.expires_date IS NULL or p.expires_date ='0000-00-00 00:00:00') and p.status != 0 then p.specials_new_products_price else p.products_price end price from ".TABLE_GLOBAL_PRODUCTS." p INNER JOIN ".TABLE_STORES." s ON s.blog_id = p.blog_id where MATCH (p.products_name,p.products_description) AGAINST ('%".$search_key."%') ".$country_q." ".$zone." ".$currency_type." ".$search_cat." and p.display_product = '1' and p.products_status = '1' ".$duration." ".$product_type." ".$price_range." order by p.products_date_added DESC, p.products_name"; 
if (!mysql_num_rows(mysql_query($catglobal_sql))) { 
$catglobal_sql = "select p.*, case when p.specials_new_products_price >= 0.0000 and (p.expires_date > Now() or p.expires_date IS NULL) and p.status != 0 then p.specials_new_products_price else p.products_price end price from ".TABLE_GLOBAL_PRODUCTS." p INNER JOIN ".TABLE_STORES." s ON s.blog_id = p.blog_id where p.products_name like '%".$search_key."%' or p.products_description like '%".$search_key."%' ".$country_q." ".$zone." ".$currency_type." ".$search_cat." and p.display_product = '1' and p.products_status = '1' ".$duration." ".$product_type." ".$price_range." order by p.products_date_added DESC, p.products_name"; 
} 

都說,即時搜索「Indifi」,這是可用的數據庫,該Indifi的global_category_id有3

值正如你可以在var_dumped結果看到,global_category_id是15,從而「 Indifi「不應該顯示,但它確實。

我的查詢應該是什麼問題?

回答

0

使用try括號這裏:

...where (p.products_name like '%indifi%' 
or p.products_description like '%indifi%') 
and s.countries_id = '168' 
... 

所以你不是OR -ing太多在一起。

+0

我沒有看到..謝謝。 – Ken

0
where p.products_name like '%indifi%' OR p.products_description like '%indifi%' and s.countries_id = '168' and global_category_id = '15' and p.display_product = '1' and p.products_status = '1' 

我看到一個OR有,所以如果p.products_name like '%indifi%',那麼它什麼都無所謂global_category_id是。

要修復優先級,請使用括號(... OR ...)

+0

我沒有看到..謝謝。 – Ken