查询时的Codeigniter,Join和Case

时间:2013-05-28 09:02:36

标签: php mysql database 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 个答案:

答案 0 :(得分: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;)

周围添加括号

答案 1 :(得分: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;

如果要转义表名和字段,可以使用“`”

更新:替换案例:

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
)

答案 2 :(得分: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;";

答案 3 :(得分: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时包装列名和表名。列名称.

中排除点\

使用`tablename`。`column_name`

答案 4 :(得分:0)

您只需在where子句中添加此条件
核心查询

(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)");

答案 5 :(得分:0)

这是我为解决系统中需要案例的问题所做的一个示例,并且codeigniter在查询中插入`并返回错误。我的解决方案是将括号 => ()的情况。

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