How to get data from parent and child table on the basis of status where foreign key have different status for every row(如何根据外键每行具有不同状态的状态从父表和子表中获取数据)
问题描述
我有 2 个具有外键关系的表.情况是我有一个 case 和一个 case 有很多 revision 并且每个 revision 都有自己的 状态.如果仅更改外键表状态的特定行,我想获取父表数据和子数据
I have 2 tables with foreign key relationship. Situation is I have a case and a case have many revisions and every revision have its own status. I want to get parent table data and child data if only specific row of foreign key table status is changed
表格案例
id      case_no    patient_name  age
1       12564        abc         78
2       1256         lkj         63
3       125          bdhf        23
表格Case_Revisons
    id      case_id    revison         status
    1         1           0           assesemnt
    2         1           1           assesment
    3         1           2           treatment
    4         2           2           assesment
    5         3           1           assesment
我想要的是来自 Case 和 Case Revisions 表的所有数据,其中状态为 treatment 
What I want is all data all data from Case and Case Revisions table where status is treatment 
我尝试了什么:
$data['treatment_setup_cases'] = MedicalPrimaryCases::with('primaryCaseNo')
            ->where('impression_type', 1)
            ->where('status', 'treatment-setup')
            ->get();
 public function primaryCaseNo()
    {
        return $this->belongsTo(PrimaryCaseNo::class, 'primary_medical_case_id');
    }
推荐答案
在此处使用连接:
$the_data_you_want = Case::join('case_revisions', 'case.id', '=', 'case_revisions.case_id')
    ->where('status', 'treatment')
    ->get();
PrimaryCaseNo 和 impression_type 在问题的解释中没有提到,所以我忽略了它们,如果需要,你可以改变它.
PrimaryCaseNo and impression_type were not mentioned in the explanation of the question so I ignored them, you can change this if necessary.
希望对你有帮助.
这篇关于如何根据外键每行具有不同状态的状态从父表和子表中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何根据外键每行具有不同状态的状态从父表和子表中获取数据
				
        
 
            
        - 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
 - SQL 临时表问题 2022-01-01
 - 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
 - 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
 - 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
 - 导入具有可变标题的 Excel 文件 2021-01-01
 - 更改自动增量起始编号? 2021-01-01
 - 在SQL中,如何为每个组选择前2行 2021-01-01
 - 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
 - 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
 
						
						
						
						
						
				
				
				
				