2016-04-29 41 views
0

如何更改我的查詢以選擇所有未共享的數據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(); 
+0

在哪些代碼中您正在翻譯SQL? – scaisEdge

+0

對不起,即時通訊使用codeigniter – Beep

+0

,因爲它是一個選擇語句,你可以只發送原始的SQL? –

回答

0

你想要做的是兩組之間的差異,通常這與左連接和一個空,其中右側表的外鍵子句(一些數據庫提供交叉functionnality但自從做了我不知道你使用的數據庫我會堅持到什麼將在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方法和一個方法來檢查如果一列是空的。

順便說一句,嘗試堅持爲您的表和列名稱的唯一命名和案例方案

+0

謝謝你,我給了這個fiddel。看到它我可以得到它的工作。我想il仍然需要'$ this-> session-> userdata(「StaffID」)'向我的用戶顯示數據 – Beep

0

匹配也許這是否行得通呢?

$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(); 
相關問題