0

尝试执行此查询。

$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
4

6 回答 6

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;)周围添加括号

于 2013-05-28T09:06:09.863 回答
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
)
于 2013-05-28T09:06:37.067 回答
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;";
于 2013-05-28T09:06:51.760 回答
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`

于 2013-05-28T09:33:29.187 回答
0

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

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

于 2018-01-16T16:59:09.583 回答
0

嗨,只需在 where 子句
CORE QUERY中添加此条件

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

CI查询

$this->db->where("(CASE WHEN tbl_account.account_type = 4 THEN  1 ELSE tbl_acc_company.acc_comp_status = 'ACTIVE' END)");
于 2016-08-12T08:39:08.133 回答