与wordpress查询的MariaDB SQL错误

时间:2018-06-01 11:29:19

标签: sql mariadb

我在MariaDB服务器上遇到SQL错误,我不明白为什么。

SQL是:

 // Check if there is posts before our $date
    $postA =$wpdb->get_row("
SELECT ID, post_date 
FROM {$wpdb->prefix}posts 
LEFT JOIN $wpdb->term_relationships ON
($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON
($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE   $wpdb->posts.post_status = IN ('publish', 'future')
AND $wpdb->posts.post_type = 'post'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id = 18
AND $wpdb->posts.post_date <'{$date}' ORDER BY post_date DESC LIMIT 1");

,错误信息为:

  

您的SQL语法有错误;检查手册   对应于您的MariaDB服务器版本,以获得正确的语法   靠近&#39; IN(&#39;发布&#39;)和wp_posts.post_type =&#39;发布&#39;和   wp_term_taxonomy.taxonomy =&#39;在第7行

1 个答案:

答案 0 :(得分:0)

此语法错误

  

在'IN('发布')附近使用的语法和wp_posts.post_type ='post'AND   wp_term_taxonomy.taxonomy ='第7行'

将参考本部分:

WHERE   $wpdb->posts.post_status = IN ('publish', 'future')
AND $wpdb->posts.post_type = 'post'
AND $wpdb->term_taxonomy.taxonomy = 'category'

$wpdb->posts.post_status是一个PHP变量,可能为空,导致无效的SQL Sentence导致:

WHERE IN ('publish') AND wp_posts.post_type = 'post' AND wp_term_taxonomy.taxonomy 

分开IN('publish')必须没有=

像:

WHERE wp_posts.post_status IN('publish')

在SQL语法错误的情况下,它非常有用,可以显示整个SQL句子。