laravel 查询构建器查询有什么问题?
What is wrong with laravel query builder query?
我编写了一个查询,使用 laravel 查询生成器从 mysql 数据库中获取数据。查看下面给出的查询生成器代码:
$products = DB::table("products as p")
->select("p.*")
->join("product_tag as pt", "pt.p_id", "p.id")
->whereIn("pt.tag_name", function($q1) use($request){
$q1->from("user_questionnaire as uc")
->select(DB::raw("distinct(at.prod_tag)"))
->join("questionnaire_answers as qa", function($join){
$join->on("qa.question_id", "=", "uc.question_id")
->where("qa.answer_number", "=", "uc.answer_id");
})
->join("answer_tags as at", "at.answer_id", "qa.id")
->where("uc.user_id", $request->user_id);
})->get();
当我记录这个查询生成器时,我得到以下响应:
[
{
"query": "select `p`.* from `products` as `p` inner join `product_tag` as `pt` on `pt`.`p_id` = `p`.`id` where `pt`.`tag_name` in (select distinct(at.prod_tag) from `user_questionnaire` as `uc` inner join `questionnaire_answers` as `qa` on `qa`.`question_id` = `uc`.`question_id` and `qa`.`answer_number` = ? inner join `answer_tags` as `at` on `at`.`answer_id` = `qa`.`id` where `uc`.`user_id` = ?)",
"bindings": [
"uc.answer_id",
115
],
"time": 0.43
}
]
现在,当我 运行 在 phpmyadmin 中进行此查询时,它 returns 需要结果。但是当 print_r $products
变量时,它显示空数组([]
)。
请指出我在查询生成器中做错了什么。
您的问题是您正在使用 ->where()
将额外条件应用于最内层的连接:
->where("qa.answer_number", "=", "uc.answer_id");
在这种情况下,第三个参数作为字符串绑定到查询中,因此您的数据库会将 qa.answer_number
字段与字符串 uc.answer_id
进行比较,这可能不是您想要的寻找。当您执行 where
时,第三个(或第二个,如果您省略运算符)将始终添加到查询绑定中,这就是导致此行为的原因。
要解决这个问题,您应该使用另一个 ->on(...)
来为连接添加额外的条件:
$join->on("qa.question_id", "=", "uc.question_id")
->on("qa.answer_number", "=", "uc.answer_id");
这将确保数据库将列与列进行比较,而不是将列与值进行比较。
我编写了一个查询,使用 laravel 查询生成器从 mysql 数据库中获取数据。查看下面给出的查询生成器代码:
$products = DB::table("products as p")
->select("p.*")
->join("product_tag as pt", "pt.p_id", "p.id")
->whereIn("pt.tag_name", function($q1) use($request){
$q1->from("user_questionnaire as uc")
->select(DB::raw("distinct(at.prod_tag)"))
->join("questionnaire_answers as qa", function($join){
$join->on("qa.question_id", "=", "uc.question_id")
->where("qa.answer_number", "=", "uc.answer_id");
})
->join("answer_tags as at", "at.answer_id", "qa.id")
->where("uc.user_id", $request->user_id);
})->get();
当我记录这个查询生成器时,我得到以下响应:
[
{
"query": "select `p`.* from `products` as `p` inner join `product_tag` as `pt` on `pt`.`p_id` = `p`.`id` where `pt`.`tag_name` in (select distinct(at.prod_tag) from `user_questionnaire` as `uc` inner join `questionnaire_answers` as `qa` on `qa`.`question_id` = `uc`.`question_id` and `qa`.`answer_number` = ? inner join `answer_tags` as `at` on `at`.`answer_id` = `qa`.`id` where `uc`.`user_id` = ?)",
"bindings": [
"uc.answer_id",
115
],
"time": 0.43
}
]
现在,当我 运行 在 phpmyadmin 中进行此查询时,它 returns 需要结果。但是当 print_r $products
变量时,它显示空数组([]
)。
请指出我在查询生成器中做错了什么。
您的问题是您正在使用 ->where()
将额外条件应用于最内层的连接:
->where("qa.answer_number", "=", "uc.answer_id");
在这种情况下,第三个参数作为字符串绑定到查询中,因此您的数据库会将 qa.answer_number
字段与字符串 uc.answer_id
进行比较,这可能不是您想要的寻找。当您执行 where
时,第三个(或第二个,如果您省略运算符)将始终添加到查询绑定中,这就是导致此行为的原因。
要解决这个问题,您应该使用另一个 ->on(...)
来为连接添加额外的条件:
$join->on("qa.question_id", "=", "uc.question_id")
->on("qa.answer_number", "=", "uc.answer_id");
这将确保数据库将列与列进行比较,而不是将列与值进行比较。