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
(...)

そして、その価値のために、codeigniter にactiverecordを使用してみてください。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

こんにちは、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 に答える