【问题标题】:call value from another table when the value is not null当值不为空时从另一个表调用值
【发布时间】:2020-05-17 09:33:32
【问题描述】:

如果有值,我想从表 stage_cdl 中获取值,如果有值,我想从故事 stage_vdl 中获取值。

在此之前,我使用了这段代码(如下),但我只收到数组,直到值 5 不包括表 stage_vdl_id

 $data = DB::table('stud_stage_cdl as ssc')
        ->join('license_cdl_module as lcm', 'lcm.id', '=', 'ssc.l_cdl_m_id')
        ->join('invoice_stud as is', 'is.id', '=', 'ssc.inv_stud_id')
        ->where('ssc.stud_id', $postData['stud_id'])
        ->select(
            'lcm.class_code',
            'is.invoice_num',
            'is.total',
            'is.created_at'
        )
        ->get();

输出:

    (
        [0] => stdClass Object
            (
                    [class_code] => D
                    [invoice_num] => W1007INV20051
                    [total] => 1484.00
                    [created_at] => 2020-05-15 10:18:38
            )

        [1] => stdClass Object
            (
                [class_code] => B2
                [invoice_num] => W1007INV20052
                [total] => 1484.00
                [created_at] => 2020-05-15 10:18:55
            )

        [2] => stdClass Object
            (
                [class_code] => DA
                [invoice_num] => W1007INV20053
                [total] => 1484.00
                [created_at] => 2020-05-15 13:44:54
            )

        [3] => stdClass Object
            (
                [class_code] => B2
                [invoice_num] => W1007INV20054
                [total] => 1484.00
                [created_at] => 2020-05-15 13:48:18
            )

        [4] => stdClass Object
            (
                [class_code] => B2
                [invoice_num] => W1007INV20055
                [total] => 1484.00
                [created_at] => 2020-05-15 14:00:54
            )

    )

invoice_stud 表

stud_stage_cdl 表

stud_stage_vdl 表

license_cdl_module 表

license_vdl_module 表

这是我到目前为止所做的,没有错误但没有值输出。我对 sql 很陌生。如您所见,我正在尝试从 lcm 和 lvm 获取值。

$data = DB::table('invoice_stud as is')
            ->join('stud_stage_vdl as ssv', 'is.stud_id', '=', 'ssv.stud_id')
            ->join('stud_stage_cdl as ssc', 'is.stud_id', '=', 'ssc.stud_id')
            ->join('license_cdl_module as lcm', 'ssc.l_cdl_m_id', '=', 'lcm.id')
            ->join('license_vdl_module as lvm', 'ssv.l_vdl_m_id', '=', 'lvm.id')
            ->where('is.stud_id', $postData['stud_id'])
            ->select(
                'lcm.class_code',
                'lvm.class_code',
                'is.invoice_num',
                'is.stage_cdl_id',
                'is.stage_vdl_id',
                'is.total',
                'is.created_at'
            )
            ->get();

【问题讨论】:

    标签: php sql select


    【解决方案1】:

    使用COALESCE,并从stage_cdlstage_vdl 中选择第一个非NULL 值(按此顺序):

    $data = DB::table('invoice_stud as is')
            ->join('stud_stage_vdl as ssv', 'is.stud_id', '=', 'ssv.stud_id')
            ->join('stud_stage_cdl as ssc', 'is.stud_id', '=', 'ssc.stud_id')
            ->join('license_cdl_module as lcm', 'ssc.l_cdl_m_id', '=', 'lcm.id')
            ->join('license_vdl_module as lvm', 'ssv.l_vdl_m_id', '=', 'lvm.id')
            ->where('is.stud_id', $postData['stud_id'])
            ->select(
                'lcm.class_code',
                'lvm.class_code',
                'is.invoice_num',
                DB::raw('COALESCE(is.stage_cdl_id, is.stage_vdl_id) AS stage_id'),
                'is.total',
                'is.created_at'
            )
            ->get();
    

    【讨论】:

    • 现在它显示值,但所有值 class_code 与 license_cdl_module 中的值相同
    • 根据您提供的有限数据和描述,我的回答是我能给出的最佳答案。如果您需要更多帮助,我建议您在问题中添加更多数据。
    【解决方案2】:

    我已经通过将它们作为一个数组来解决这个问题。

       $data['LicenseCDL'] = DB::table('stud_stage_cdl as ssc')
            ->join('license_cdl_module as lcm', 'lcm.id', '=', 'ssc.l_cdl_m_id')
            ->join('invoice_stud as is', 'is.id', '=', 'ssc.inv_stud_id')
            ->where('ssc.stud_id', $stud_id)
            ->select(
                'lcm.class_code',
                'is.total',
                'is.created_at'
            )
            ->get();
    
        $data['LicenseVDL'] = DB::table('stud_stage_vdl as ssv')
            ->join('license_vdl_module as lvm', 'lvm.id', '=', 'ssv.l_vdl_m_id')
            ->join('invoice_stud as is', 'is.id', '=', 'ssv.inv_stud_id')
            ->where('ssv.stud_id', $stud_id)
            ->select(
                'lvm.class_code',
                'is.total',
                'is.created_at'
            )
            ->get();
    
    error_log($data); //Will receive all of them 
    

    【讨论】:

      猜你喜欢
      • 2013-04-06
      • 2022-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-29
      • 2022-08-18
      • 2021-01-01
      相关资源
      最近更新 更多