发现很难使SQL查询

时间:2018-11-12 10:01:13

标签: mysql database

我检查了数据以简单数组的形式进入$ posts_not_to_select中,其他一切都很好,但显示出以下错误:

  

异常#0(Zend_Db_Statement_Exception):SQLSTATE [42000]:语法   错误或访问冲突:1064您的SQL语法有错误;   检查与您的MySQL服务器版本相对应的手册   在'WHERE entity_id NOT IN附近使用正确的语法(选择post_id   在第1行的FROM mst_blog_store_post“不存储”的地方,查询是:SELECT   *从mst_blog_post_entitycreated_at的命令范围内,entity_id不在1范围内(从mst_blog_store_post中选择post_id不在这里   store_id = 1)

$posts_not_to_selectssql1 = "SELECT post_id FROM mst_blog_store_post WHERE NOT store_id = $storeID";
    $posts_not_to_select12 = $connection->fetchAll($posts_not_to_selectssql1);
    $posts_not_to_select = array();

    foreach ($posts_not_to_select12 as $key => $value){
        $posts_not_to_select[$key] = $value['post_id'];
    }

    "SELECT * FROM `mst_blog_post_entity` ORDER BY `created_at` ASC LIMIT 1 WHERE `entity_id` NOT IN ( '" . implode( "', '" , $posts_not_to_select ) . "' )"

2 个答案:

答案 0 :(得分:1)

SELECT * FROM `mst_blog_post_entity` WHERE `entity_id` NOT IN ( '" . implode( "', '" , $posts_not_to_select ) . "' )" ORDER BY `created_at` ASC LIMIT 1;

只需在phpmyadmin的SQL部分中运行此查询,一切就可以正常工作。

答案 1 :(得分:0)

ORDER BYWHERE子句之后。

SELECT * FROM `mst_blog_post_entity` 
WHERE `entity_id` NOT IN ( '" . implode( "', '" , $posts_not_to_select ) . "' )"  
ORDER BY `created_at` ASC LIMIT 1 

完整序列为:

  • 选择
  • 加入[开]
  • 在哪里
  • 组别
  • 拥有
  • 按[LIMIT]订购