2013-03-27 94 views
0

我在這裏張貼手動閱讀很多類似的線程之後,但不解決我的問題,所以我需要MySQL的高手幫我找出什麼地方錯了這個查詢:格式化MySQL查詢:您的SQL語法中有錯誤;檢查對應於你的MySQL服務器版本

的查詢如下所示:

SELECT DISTINCT(p.ID), p.*, pm.*, t.*, tt.*, tr.* 
FROM wp_posts p, wp_postmeta pm, wp_terms as t, wp_term_taxonomy as tt, wp_term_relationships as tr 
'WHERE p.ID = pm.post_id 
AND t.term_id = tt.term_id 
AND tt.taxonomy = wpsc_product_category 
AND tt.term_taxonomy_id = tr.term_taxonomy_id 
AND tr.object_id = p.ID 
AND p.post_type = wpsc-product 
AND pm.meta_key != _wpsc_price 
AND (p.post_title LIKE '%'\"some string to search\"'%' 
    OR p.post_content LIKE '%'\"some string to search\"'%' 
    OR pm.meta_value = '\"some string to search\"' 
    OR t.name LIKE '%'\"some string to search\"'%') 
GROUP BY p.ID ORDER By p.ID ' 

我沒有什麼錯的查詢格式得到以下錯誤:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''WHERE p.ID = pm.post_id AND t.term_id = tt.term_id AND tt.taxonomy = wpsc_produ' at line 1 

在我的查詢,我需要整合的字符串,而我是個迫切所以我不能完全改變PDO。我怎樣才能解決這個錯誤?提前Thanx。

PS:我是本地服務器上運行,因此PHP的信息顯示我關於MySQL服務器的信息,我想它的版本:

Client API version 5.5.29 

回答

0

根據錯誤消息,您在WHERE子句之前有額外的單引號。

作爲旁註,DISTINCT關鍵字更有可能是無用的,因爲它只有在所有列的所有值在兩行上都相等的情況下才起作用,否則該行即使具有相同的值也是唯一的。

0

你不需要WHERE子句周圍引號。

0

您應該始終在查詢中保持相同的約定,以使其更具可讀性並且不太可能出現錯誤。

這不會影響結果,但如果您使用別名「wp_posts p」或「wp_posts AS p」是等同的,但您混合使用此查詢。我已經爲你制定了慣例並修正了下面的查詢。

SELECT DISTINCT(p.ID), p.*, pm.*, t.*, tt.*, tr.* 
FROM wp_posts p, wp_postmeta pm, wp_terms t, wp_term_taxonomy tt, wp_term_relationships tr 
WHERE p.ID = pm.post_id 
AND t.term_id = tt.term_id 
AND tt.taxonomy = wpsc_product_category 
AND tt.term_taxonomy_id = tr.term_taxonomy_id 
AND tr.object_id = p.ID 
AND p.post_type = wpsc-product 
AND pm.meta_key != _wpsc_price 
AND (p.post_title LIKE "%some string to search%" 
    OR p.post_content LIKE "%some string to search%" 
    OR pm.meta_value = "some string to search" 
    OR t.name LIKE "%some string to search%") 
GROUP BY p.ID ORDER By p.ID 
相關問題