比较并显示不匹配的数据


compare and show data that does not match

如何更改查询以选择所有不共享ReportID的数据?

所以所有'report.Report_Name, report.ReportDate, report.ReportID'report.ReportID = Read_Report.ReportID 不匹配

然后从report 中获取所有不匹配的内容

查询

 $this->db->select('report.Report_Name, report.ReportDate, report.ReportID')
            ->from('report')
            ->join('Read_Report', 'report.ReportID = Read_Report.ReportID')
            ->where('Read_Report.StaffID', $this->session->userdata("StaffID"));
        $result = $this->db->get();
        return $result->result();

您想要做的是两个集合之间的区别,通常这是通过左联接和右表外键上的null where子句来完成的(有些数据库提供了intersect函数,但由于我不知道您使用的是哪个数据库,我将坚持使用任何SQL风格的数据库)

所以你的查询应该是这样的:

SELECT report.Report_name, report.ReportDate, report.ReportId
FROM report LEFT JOIN Read_Report ON report.ReportId=Read_Report.ReportId
WHERE Read_Report.ReportId IS NULL

我不知道querybuilder的具体语法,但我认为您将有一个leftJoin方法和一种检查列是否为null的方法。

顺便说一句,试着为您的表和列名

使用唯一的命名和大小写方案

也许这会奏效?

    $this->db->select('report.Report_Name, report.ReportDate, report.ReportID')
        ->from('report')
        ->join('Read_Report', 'report.ReportID = Read_Report.ReportID')
        ->where('Read_Report.StaffID !=', $this->session->userdata("StaffID"));
    $result = $this->db->get();
    return $result->result();