Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
除了某个学生已经在laravel MySQL中购买的论文之外,获取所有论文_Mysql_Laravel_Join - Fatal编程技术网

除了某个学生已经在laravel MySQL中购买的论文之外,获取所有论文

除了某个学生已经在laravel MySQL中购买的论文之外,获取所有论文,mysql,laravel,join,Mysql,Laravel,Join,我有一个数据库与学生,论文和付款表,我需要得到所有的文件,学生已经买了以前。下面是我的迁移示例- 纸 -付款 Schema::create('payments', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned()->index(); $table->foreign('user_id

我有一个数据库与学生,论文和付款表,我需要得到所有的文件,学生已经买了以前。下面是我的迁移示例- 纸

-付款

Schema::create('payments', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned()->index();
        $table->foreign('user_id')->references('id')->on('users');
        $table->integer('paper_id')->unsigned()->index();
        $table->foreign('paper_id')->references('id')->on('papers');
        $table->float('amount');
        $table->timestamps();
    });
我试过的代码给了我错误的数据

     $papers = DB::table('papers')
    ->leftJoin('payments','payments.paper_id','=','papers.id')
    ->orderBy('papers.price','ASC')
    ->select('papers.*')
    ->where('papers.id','<>','payments.paper_id')
    ->get();
$papers=DB::table('papers')
->leftJoin('payments','payments.paper_id','=','papers.id'))
->订购人('papers.price','ASC')
->选择('papers.*')
->其中('papers.id','','payments.papers\u id')
->get();

任何人都可以帮忙。

你可以用where-raw这样做

DB::table('papers')->whereRaw('id Not in (Select paper_id FROM payments)')->get();
DB::table('papers')->whereRaw('id NOT IN (Select paper_id FROM payments where stduent_id='.$student_id.')')->get();
或者您也可以添加学生id以获得如下优化结果

DB::table('papers')->whereRaw('id Not in (Select paper_id FROM payments)')->get();
DB::table('papers')->whereRaw('id NOT IN (Select paper_id FROM payments where stduent_id='.$student_id.')')->get();

表架构和查询非迁移的Post sql语法,如果可能,还有示例数据您想要什么样的最终结果?对于编辑问题,很抱歉。我问错了question@ChamaraAbeysekara我需要看看学生还没有买的试卷的记录