【问题标题】:Laravel DB query builder with LEFT JOIN using a RAW expressionLaravel DB 查询构建器,使用 RAW 表达式进行 LEFT JOIN
【发布时间】:2018-07-05 11:52:33
【问题描述】:

我想 left join 来自 many-to-many 关系的数据。在我第一次尝试解决问题时,我遇到了How can a LEFT OUTER JOIN return more records than exist in the left table? 中描述的情况

现在我认为我在执行left join 之前连接了many-to-many 关系中的值,但我又被卡住了,没有得到任何结果。我设法提出了部分解决方案,但我不知道如何将它们组合成最终解决方案。

查询

SELECT 
    groups.id as `groups.id`, 
    GROUP_CONCAT(choices.text) AS choices
FROM `input-choices` choices
    LEFT JOIN `input-choice_input-group` pivot ON choices.id = `pivot`.`input-choice_id`
    LEFT JOIN `input-groups` groups ON groups.id = `pivot`.`input-group_id`
WHERE groups.id = 2
GROUP BY groups.id

WHERE groups.id = 2 最终将更改为 WHERE groups.id = `input-types`.`input-group.id`

产生e。 G。结果集如下:

groups.id | choices 
2         | 1,2,3,4,5

作为下一步,我认为使用 Query Builder DB::raw() 方法左连接上一个查询的结果是有意义的,但我又卡住了。

$survey = DB::table('questions')
->join('question_survey', function ($join) {
    $join->on('questions.id', '=', 'question_survey.question_id')
        ->where('question_survey.survey_id', '=', 1);
})
->leftJoin('answer_question_survey', 'question_survey.id', '=', 'answer_question_survey.question_survey_id')
->leftJoin('answers', 'answer_question_survey.answer_id', '=', 'answers.id')
->leftJoin('input-types', 'input-types.id', '=', 'questions.input-type_id')
->leftJoin(DB::raw(...), 'input-types.input-group.id', '=', '?.groups.id')
->select(
    'questions.id AS question.id',
    'questions.text AS question.text',
    'questions.explanation AS question.explanation',
    'questions.is_required AS question.is_required',
    'questions.input-type_id AS question.input-type_id',
    'input-types.id AS input-type.id',
    'input-types.name AS input-type.name',
    'input-types.input-group_id AS input-type.input-group_id',
    'question_survey.id AS question_survey.id',
    'question_survey.question_id AS question_survey.question_id',
    'question_survey.survey_id AS question_survey.survey_id',
    'question_survey.parent_id AS question_survey.parent_id',
    'answer_question_survey.id AS answer_question_survey.id',
    'answer_question_survey.question_survey_id AS answer_question_survey.question_survey_id',
    'answer_question_survey.answer_id AS answer_question_survey.answer_id',
    'answers.id AS answer.id',
    'answers.text AS answer.text'
)
->get();
  1. 如何在DB::raw() 方法中使用第一个查询。我总是收到诸如“语法错误或访问冲突:...”之类的错误
  2. 我将如何完成连接条件'input-types.input-group.id', '=', '?.groups.id',因为我不知道我加入的表的名称。

【问题讨论】:

    标签: mysql laravel eloquent laravel-eloquent laravel-query-builder


    【解决方案1】:

    尝试将您的子查询作为 DB::raw() 放入选择部分。

    【讨论】:

    • 感谢您的快速跟进。我试图实施您的解决方案,但不知何故它不起作用。为了快速测试,我使用... 'answers.text AS answer.text', DB::raw('SELECT * FROM [BACKTICK]input-types[BACKTICK]'))->get();... 扩展了查询,但我只收到错误消息语法错误或访问冲突:1064 您的SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'qu' 上的 'SELECT * FROM input-types from questions inner join question_survey 附近使用正确的语法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    • 2018-08-18
    • 1970-01-01
    • 2014-04-17
    相关资源
    最近更新 更多