2013-05-28 77 views
0

嘗試執行此查詢。Codeigniter,查詢時加入和查詢

$sql ="SELECT '*' FROM 
      'osp_job_details' 
       LEFT JOIN 
      'osp_job_status_track' ON 'osp_job_status_track'.'JobID' = 'osp_job_details'.'JobID' 
       LEFT JOIN 
      'osp_job_status' ON 'osp_job_status'.'StatusID' = 'osp_job_status_track'.'StatusID' 
       LEFT JOIN 
      'osp_job_sub_status' ON 'osp_job_sub_status'.'SubStatusID' = 'osp_job_status_track'.'SubStatusID' 
       LEFT JOIN 
      'hr_employee_details' ON 'hr_employee_details'.'EmployeeID' = 'osp_job_details'.'AssignToEmployeeID' 
       LEFT JOIN 
      'osp_job_type' ON 'osp_job_type'.'JobTypeID' = 'osp_job_details'.'JobtypeID' 
      WHERE 'isDefault' = 0 AND 

      CASE WHEN 'osp_job_status'.'StatusID' = '2' 
      THEN 'osp_job_sub_status'.'CurrentStatus' = '3' 
      ELSE 'osp_job_status'.'StatusID' >= '2' 
       END;"; 

     $query = $this->db->query($sql); 
     return $query->result(); 

但我在運行上面的查詢時遇到了下面的錯誤。

Error Number: 1064 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''osp_job_details' LEFT JOIN 'osp_job_status_track'' at line 2 

SELECT '*' FROM 'osp_job_details' LEFT JOIN 'osp_job_status_track' ON 'osp_job_status_track'.'JobID' = 'osp_job_details'.'JobID' LEFT JOIN 'osp_job_status' ON 'osp_job_status'.'StatusID' = 'osp_job_status_track'.'StatusID' LEFT JOIN 'osp_job_sub_status' ON 'osp_job_sub_status'.'SubStatusID' = 'osp_job_status_track'.'SubStatusID' LEFT JOIN 'hr_employee_details' ON 'hr_employee_details'.'EmployeeID' = 'osp_job_details'.'AssignToEmployeeID' LEFT JOIN 'osp_job_type' ON 'osp_job_type'.'JobTypeID' = 'osp_job_details'.'JobtypeID' WHERE 'isDefault' = 0 AND CASE WHEN 'osp_job_status'.'StatusID' = '2' THEN 'osp_job_sub_status'.'CurrentStatus' = '3' ELSE 'osp_job_status'.'StatusID' >= '2' END; 

Filename: C:\xampp\htdocs\projects\zorkif_new\system\database\DB_driver.php 

Line Number: 330 

有人可以告訴我的查詢出了什麼問題,以及如何解決它?

++++++++++++++++++++++++++++++++++++++++++++++ ++ 更新:

刪除了單引號

$sql ="SELECT * FROM 
    osp_job_details 
     LEFT JOIN 
    osp_job_status_track ON osp_job_status_track.JobID = osp_job_details.JobID 
     LEFT JOIN 
    osp_job_status ON osp_job_status.StatusID = osp_job_status_track.StatusID 
     LEFT JOIN 
    osp_job_sub_status ON osp_job_sub_status.SubStatusID = osp_job_status_track.SubStatusID 
     LEFT JOIN 
    hr_employee_details ON hr_employee_details.EmployeeID = osp_job_details.AssignToEmployeeID 
     LEFT JOIN 
    osp_job_type ON osp_job_type.JobTypeID = osp_job_details.JobtypeID 
    WHERE isDefault = 0 AND 

    CASE WHEN osp_job_status.StatusID = 2 
    THEN osp_job_sub_status.CurrentStatus = 3 
    ELSE osp_job_status.StatusID >= 2 
     END;"; 

但現在收到這個錯誤如下..

Error Number: 1064 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'isDefault` = 1 AND CASE WHEN `osp_job_status`.`StatusID` = 2 THEN `osp_job' at line 8 

SELECT `osp_job_details`.*, `osp_job_type`.`JobTypeName`, `status`, `Substatus`, `osp_job_status`.`StatusID`, `osp_job_sub_status`.`SubStatusID`, `FirstName`, `MiddleNames`, `LastName`, `hr_employee_details`.`EmployeeID`, `osp_job_status_track`.`StatusTrackID` FROM (`osp_job_details`) LEFT JOIN `osp_job_status_track` ON `osp_job_status_track`.`JobID` = `osp_job_details`.`JobID` LEFT JOIN `osp_job_status` ON `osp_job_status`.`StatusID` = `osp_job_status_track`.`StatusID` LEFT JOIN `osp_job_sub_status` ON `osp_job_sub_status`.`SubStatusID` = `osp_job_status_track`.`SubStatusID` LEFT JOIN `hr_employee_details` ON `hr_employee_details`.`EmployeeID` = `osp_job_details`.`AssignToEmployeeID` LEFT JOIN `osp_job_type` ON `osp_job_type`.`JobTypeID` = `osp_job_details`.`JobtypeID` WHERE ` ` isDefault` = 1 AND CASE WHEN `osp_job_status`.`StatusID` = 2 THEN `osp_job_sub_status`.`CurrentStatus` = 3 ELSE `osp_job_status`.`StatusID` >= 2 END ; 

Filename: C:\xampp\htdocs\projects\zorkif_new\system\database\DB_driver.php 

Line Number: 330 
+0

爲什麼使用'*',它將檢索6個表的所有列。指定要檢索的列 – diEcho

+0

將所有表或列名稱的'''(單引號)更改爲''''(在'〜下標記) – diEcho

回答

1

使用別名來簡化

$sql ="SELECT [specific column names ] FROM `osp_job_details` jd 
    LEFT JOIN 
      `osp_job_status_track` jst ON (`jst`.`JobID` = `jd`.`JobID`) 
    LEFT JOIN 
      `osp_job_status` js ON (`js`.`StatusID` = `jst`.`StatusID`) 
    LEFT JOIN 
      `osp_job_sub_status` jss ON (`jss`.`SubStatusID` = `jst`.`SubStatusID`) 
    LEFT JOIN 
      `hr_employee_details` hed ON (`hed`.`EmployeeID` = `jd`.`AssignToEmployeeID`) 
    LEFT JOIN 
      `osp_job_type` jt ON (`jt`.`JobTypeID` = `jd`.`JobtypeID`) 

    WHERE `isDefault` = '0' 
    AND CASE 
       WHEN `js`.`StatusID` = '2' THEN `jss`.`CurrentStatus` = '3' 
       ELSE `jd`.`StatusID` >= '2' 
      END "; 
    $this->db->query($qry); 

注意。列名排除點.\

使用'tablename'。`column_name`

1

只是刪除你的周圍表名引號。

SELECT * 
FROM osp_job_details 
(...) 

而對於它的價值,您應該嘗試使用activerecord作爲codeigniter。你不會有與SQL擔心:

$this->db 
    ->from('osp_job_details') 
    ->join('osp_job_status_track', 'osp_job_status_track.JobID = osp_job_details.JobID', 'left') 
    ->join('osp_job_status', 'osp_job_status.StatusID = osp_job_status_track.StatusID', 'left') 
    ->join('osp_job_sub_status', 'osp_job_sub_status.SubStatusID = osp_job_status_track.SubStatusID', 'left') 
    ->join('hr_employee_details', 'hr_employee_details.EmployeeID = osp_job_details.AssignToEmployeeID', 'left') 
    ->join('osp_job_type', 'osp_job_type.JobTypeID = osp_job_details.JobtypeID', 'left') 
    ->where('isDefault', 0) 
    ->where("osp_job_status.StatusID = '2' AND osp_job_sub_status.CurrentStatus = '3' OR osp_job_status.StatusID >= '2'") 
    ->result(); 

如果它不能正常工作,加上括號在你CASE WHEN

+0

案例CI不支持時。 –

+0

情況何時不需要。 –

1

刪除所有的單引號:

SELECT * FROM 
      osp_job_details 
       LEFT JOIN 
      osp_job_status_track ON osp_job_status_track.JobID = osp_job_details.JobID 
       LEFT JOIN 
      osp_job_status ON osp_job_status.StatusID = osp_job_status_track.StatusID 
       LEFT JOIN 
      osp_job_sub_status ON osp_job_sub_status.SubStatusID = osp_job_status_track.SubStatusID 
       LEFT JOIN 
      hr_employee_details ON hr_employee_details.EmployeeID = osp_job_details.AssignToEmployeeID 
       LEFT JOIN 
      osp_job_type ON osp_job_type.JobTypeID = osp_job_details.JobtypeID 
      WHERE isDefault = 0 AND 

      CASE WHEN osp_job_status.StatusID = 2 
      THEN osp_job_sub_status.CurrentStatus = 3 
      ELSE osp_job_status.StatusID >= 2 
       END; 

如果你想逃避表名和字段,你可以使用像「`」

UPDATE:替換案例:

CASE WHEN osp_job_status.StatusID = 2 
THEN osp_job_sub_status.CurrentStatus = 3 
ELSE osp_job_status.StatusID >= 2 
    END; 

有:

(
    (
     osp_job_status.StatusID = 2 
     AND 
     osp_job_sub_status.CurrentStatus = 3 
    ) 
    OR 
    osp_job_status.StatusID >= 2 
) 
+0

更新了我的問題,我刪除了所有單引號,但仍然出現錯誤。 –

+0

我也更新了我的答案 – Stephan

1

在您的查詢您使用的表名和列名''單一配額由它被視爲一個字符串,所以更換了``標籤的單個配額。引用表名時包列名和表名與`'也:

這裏被替換代碼:

$sql ="SELECT * FROM 
      `osp_job_details` 
       LEFT JOIN 
      `osp_job_status_track` ON `osp_job_status_track`.`JobID` = `osp_job_details`.`JobID` 
       LEFT JOIN 
      `osp_job_status` ON `osp_job_status`.`StatusID` = `osp_job_status_track`.`StatusID` 
       LEFT JOIN 
      `osp_job_sub_status` ON `osp_job_sub_status`.`SubStatusID` = `osp_job_status_track`.`SubStatusID` 
       LEFT JOIN 
      `hr_employee_details` ON `hr_employee_details`.`EmployeeID` = `osp_job_details`.`AssignToEmployeeID` 
       LEFT JOIN 
      `osp_job_type` ON `osp_job_type`.`JobTypeID` = `osp_job_details`.`JobtypeID` 
      WHERE `isDefault` = 0 AND 

      CASE WHEN `osp_job_status`.`StatusID` = '2' 
      THEN `osp_job_sub_status`.`CurrentStatus` = '3' 
      ELSE `osp_job_status`.`StatusID` >= '2' 
       END;"; 
+0

我更新了我的問題,在您的情況下,我收到了第二個錯誤,這是我在更新中提出的。 –

+0

@SyedHaiderHassan再次檢查您的查詢,然後執行它。也許你已經留下了空間,或者你需要添加列名稱爲isDefault的表名。 – 2013-05-28 09:17:57

0

嗨只需添加此條件的WHERE子句中
CORE QUERY

(CASE 
     WHEN 
      tbl_account.account_type = 4 
     THEN 
      1 
    ELSE 
    `tbl_acc_company`.`acc_comp_status` = 'ACTIVE' 
    END 
) 

CI QUERY

$this->db->where("(CASE WHEN tbl_account.account_type = 4 THEN 1 ELSE tbl_acc_company.acc_comp_status = 'ACTIVE' END)"); 
0

這就是我所做的一個實例解決我的系統中需要一個案例的問題,並插入代碼查詢中的`並返回錯誤。我的解決方案是將括號 =>()

$this->db->select("(case when c.my_column THEN c.my_column ELSE c.other_column END as my_column_name)")