【问题标题】:Laravel Query Builder join error. SQLSTATE[42000] errorLaravel 查询生成器连接错误。 SQLSTATE[42000] 错误
【发布时间】:2016-04-25 11:30:24
【问题描述】:

我正在使用 Laravel 查询构建器来编写一个连接语句,我发现我遇到了一个奇怪的错误。当我从 phpmyadmin 运行下面的查询时,它可以工作,但是当我尝试访问 Laravel 中的页面时出现错误。

SQLSTATE[42000]: Syntax error or access violation: 1064 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 `entities`.`deleted_at` is null' at line 1 (SQL: select * from `entities` inner join 
`entity_contact_info` on `entity_contact_info`.`entity_id` = `entities`.`id` and `entities`.`sector_id`
= 2 and `entity_contact_info`.`country_id` IN (select `id` from countries WHERE `region_id` = 9)
where `entities`.`deleted_at` is null)

我在 Laravel 中构建的查询如下。同样,当我从上面的错误中复制查询并运行它时,它可以工作。这似乎没有理由不起作用。

$query = Entity::Join("entity_contact_info", function ($join){
                $join->on("entity_contact_info.entity_id", "=", "entities.id")
                    ->where("entities.sector_id", "=", "2")
                    ->where("entity_contact_info.country_id", "IN", "(select `id` from countries WHERE `region_id` = 9)");
                })->get();

有什么建议吗?

【问题讨论】:

    标签: php sql laravel pdo


    【解决方案1】:

    我会说问题在于您的第二个where() 声明。

    试试下面的。

    ->where("entity_contact_info.country_id", "IN", DB::raw("(select `id` from countries WHERE `region_id` = 9)"))
    

    【讨论】:

      【解决方案2】:

      试试这个,

      $query = Entity::Join("entity_contact_info", function ($join){
                      $join->on("entity_contact_info.entity_id", "=", "entities.id")
                          ->where("entities.sector_id", "=", "2")
                          ->whereIn("entity_contact_info.country_id", DB::raw("(select `id` from countries WHERE `region_id` = 9)"));
                      })->get();
      

      另一种方式,

      $query = Entity::Join("entity_contact_info", function ($join) {
                      $join->on("entity_contact_info.entity_id", "=", "entities.id")
                      ->where("entities.sector_id", "=", "2")
                  ->whereIn('entity_contact_info.country_id', function($query) {
                      $query->select('id')
                          ->from('countries')
                          ->where('countries.region_id = 9');
                  })
              })->get();
      

      Use of where IN laravel

      【讨论】:

      • 我试过这个但我得到以下错误:Argument 2 passed to Illuminate\Database\Query\JoinClause::whereIn() must be of the type array, object given
      • @TonyOlendo 您第一次尝试还是第二次尝试了哪种方式?你可以试试这样DB::raw("(select id from countries WHERE region_id = 9)")->toArray()
      • 感谢您一直以来的帮助。我尝试了这两个建议,您最近的评论产生了以下错误:调用未定义的方法 Illuminate\Database\Query\Expression::toArray()
      • @TonyOlendo 试试DB::raw("(select id from countries WHERE region_id = 9)")->get()->toArray()
      猜你喜欢
      • 2015-07-15
      • 1970-01-01
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      • 2019-04-18
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      相关资源
      最近更新 更多