【问题标题】:Laravel Multiple table query for three tablesLaravel 多表查询三张表
【发布时间】:2017-09-11 14:58:29
【问题描述】:

我是 laravel 的新手,我创建了一个在 Xampp 服务器中完美运行的查询,现在我想在 laravel 中转换这个查询,我创建了一个显示错误 SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'question_chapter_rel' (SQL: select count(*) as aggregate fromquestion_chapter_relinner joinquestion_chapter_rel@ 的 laravel 代码987654331个@章. ID @ 987654333个@ question_chapter_rel . chapter_id inner join问题on问题. ID = question_chapter_rel . question_id inner join答案on答案. ID = questions.correct_answerswhereanswers.is_correct= 1)

这是我的 laravel 查询:

public function scopeGetQuestion($query)
    {

       return $query->join('question_chapter_rel', 'chapters.id', '=' ,'question_chapter_rel.chapter_id')
                    ->join('questions','questions.id', '=' ,'question_chapter_rel.question_id')
                    ->join('answers','answers.id', '=' ,'questions.correct_answers')
            ->select(
                [
                    'chapters.id',
                    'chapters.chapter_description',
                    'questions.id',
                    'questions.question_description',
                    'answers.id',
                    'answers.answer_description'
                ]
            )

            ->where('answers.is_correct',1)
            ->paginate(12);
    }

这是我要转换的代码

select `chapters`.`id`, `chapters`.`chapter_description`, `questions`.`id`, `questions`.`question_description`, `answers`.`id`, `answers`.`answer_description`

from `chapters`
Inner Join `question_chapter_rel`
on `chapters`.`id` = `question_chapter_rel`.`chapter_id`

Inner Join `questions`
on `questions`.`id` = `question_chapter_rel`.`question_id`

Inner Join `answers`
on `answers`.`id` = `questions`.`correct_answers`

where `answers`.`is_correct` = 1

提前致谢

question_chapter_rel 表 章节表 问题表 答案表

【问题讨论】:

    标签: mysql sql database laravel-5.2


    【解决方案1】:

    您始终可以使用 DB::raw。检查 laracast 文档中的 join 语句https://laravel.com/docs/5.4/queries#joins

    检查一下

    请添加使用数据库;

    public function scopeGetQuestion($query)
    {
        dd(DB::table('chapters')->select('chapters.id', 'chapters.chapter_description', 'questions.id', 'questions.question_description', 'answers.id', 'answers.answer_description')->join('question_chapter_rel', 'chapters.id', '=' ,'question_chapter_rel.chapter_id')->join('questions','questions.id', '=' ,'question_chapter_rel.question_id')->join('answers','answers.id', '=' ,'questions.correct_answers')->where('answers.is_correct', '=' ,1)->get());
        return;
    }
    

    【讨论】:

    • 感谢您的资源,但它根本不起作用,数据库中没有显示数据。
    • 响应更改,代码修改。检查一下,你能用数据显示表结构吗?
    • 您好,我已经更改了我的问题,添加了包含数据和错误详细信息的表格描述。
    • 谢谢我得到了解决方案
    • 代码更新 // 将章节表从连接子句移动到数据库查询中
    【解决方案2】:
    This how its works, From the first line use **chapter** instead **question_chapter_rel**
    
    
      public function scopeGetQuestion($query,$id)
            {
    
                return $query->join('chapters', 'chapters.id', '=' ,'question_chapter_rel.chapter_id')
                    ->join('questions','questions.id', '=' ,'question_chapter_rel.question_id')
                    ->join('answers','answers.id', '=' ,'questions.correct_answers')
                    ->where('answers.is_correct',1)
                    ->where('question_chapter_rel.chapter_id',$id)
                    ->select(
                        [
    
                            'chapters.id',
                            'chapters.chapter_description',
                            'questions.id',
                            'questions.question_description',
                            'answers.id',
                            'answers.answer_description'
                        ]
                    )
    
    
                ->paginate(20);
            }
    

    【讨论】:

      猜你喜欢
      • 2017-04-04
      • 1970-01-01
      • 2019-12-19
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 2015-10-20
      • 2020-08-04
      相关资源
      最近更新 更多