嘗試執行此查詢。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
爲什麼使用'*',它將檢索6個表的所有列。指定要檢索的列 – diEcho
將所有表或列名稱的'''(單引號)更改爲''''(在'〜下標記) – diEcho