0

在阅读了许多类似的线程后,我在这里发帖,但这并不能解决我的问题,所以我需要 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 为时已晚。我该如何解决这个错误?

PS:我在本地服务器上运行,所以 php info 向我显示了有关 MySQL 服务器的信息,我猜它是版本:

Client API version  5.5.29 
4

3 回答 3

0

You don't need quote marks around the WHERE clause.

于 2013-03-27T13:48:17.350 回答
0

According to the error message, you have an extra single quote before the WHERE clause.

As a sidenote, DISTINCT keyword will be more likely to be useless because it will only work if all values in all columns are equal on both row otherwise the row is unique even if they have the same value.

于 2013-03-27T13:48:25.440 回答
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
于 2013-03-27T13:58:00.210 回答