2013-03-27 100 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 
相关问题