查询在SQL Server上有效,不适用于php codeigniter

时间:2019-01-17 12:00:17

标签: php sql codeigniter

希望这个问题不是重复的。

我有一个可在sql服务器上运行的查询。当我尝试在Codeigniter中使用它时,出现内部服务器错误(500)。

这是带有php代码的查询,

   public function tnaBucketTable($filter = null){
    $sql = "
        SELECT TOP 500 start ,SC.status ,S.shift_id ,SC.call_id,SC.type,SC.win_end,S.job_id
        ,pin = CASE 
                WHEN pin IS NULL THEN pin_agency 
                ELSE pin 
            END
            ,CONVERT(VARCHAR, start, 3)+' '+CONVERT(CHAR(5), start, 108) AS start_time
            ,CONVERT(CHAR(5), [end], 108) AS finish_time
            ,guard_name = CASE guard_info
                WHEN 'Agency' THEN  (SELECT shortname FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency)
                ELSE (SELECT shortname FROM [SecurityPlatform].[dbo].[SP-Guard] WHERE pin = S.pin)
            END
            ,guard_number = CASE guard_info
                WHEN 'Agency' THEN  (SELECT phone1 FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency)
                ELSE (SELECT phone_number FROM [SecurityPlatform].[dbo].[SP-Guard] WHERE pin = S.pin)
            END
            ,agency_number = CASE S.guard_info
                WHEN 'Agency' THEN (
                    SELECT contact_no_1 FROM [SecurityPlatform].[dbo].[SP-Agency] WHERE agency_id = (
                        SELECT agency_id FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency
                    )
                )
                ELSE NULL
            END
            ,C.client_name
            ,ST.site_name
            ,ST.phone_number AS site_phone
            ,ST.site_sin
            FROM [SecurityPlatform].[dbo].[SP-Shift] S
            JOIN [SecurityPlatform].[dbo].[SP-Job] J ON J.job_id = S.job_id
            JOIN [SecurityPlatform].[dbo].[SP-Site] ST ON ST.site_sin = J.[sin]
            JOIN [SecurityPlatform].[dbo].[SP-Client] C ON C.client_id = ST.client_id
            JOIN [SecurityPlatform].[dbo].[SP-ScheduleCall] SC ON SC.shift_id = S.shift_id 
            JOIN [SecurityPlatform].[dbo].[SP-TrackingCall] TC ON TC.call_id = SC.call_id
            WHERE S.[status] = 'Confirmed'
            AND SC.[status] != 'Unconfirmed'
            AND pin > 0 ORDER BY S.shift_id DESC, SC.win_end DESC";

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

以及来自Sql Server的结果:

result

浏览器的结果:

enter image description here

有人知道我想念什么吗?

请询问是否需要更多信息。

谢谢。

编辑:var_dump($ sql)进行请求

string(1763) " SELECT TOP 500 start ,SC.status ,S.shift_id ,SC.call_id,SC.type,SC.win_end,S.job_id ,pin = CASE WHEN pin IS NULL THEN pin_agency ELSE pin END ,CONVERT(VARCHAR, start, 3)+' '+CONVERT(CHAR(5), start, 108) AS start_time ,CONVERT(CHAR(5), [end], 108) AS finish_time ,guard_name = CASE guard_info WHEN 'Agency' THEN (SELECT shortname FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency) ELSE (SELECT shortname FROM [SecurityPlatform].[dbo].[SP-Guard] WHERE pin = S.pin) END ,guard_number = CASE guard_info WHEN 'Agency' THEN (SELECT phone1 FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency) ELSE (SELECT phone_number FROM [SecurityPlatform].[dbo].[SP-Guard] WHERE pin = S.pin) END ,agency_number = CASE S.guard_info WHEN 'Agency' THEN ( SELECT contact_no_1 FROM [SecurityPlatform].[dbo].[SP-Agency] WHERE agency_id = ( SELECT agency_id FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency ) ) ELSE NULL END ,C.client_name ,ST.site_name ,ST.phone_number AS site_phone ,ST.site_sin FROM [SecurityPlatform].[dbo].[SP-Shift] S JOIN [SecurityPlatform].[dbo].[SP-Job] J ON J.job_id = S.job_id JOIN [SecurityPlatform].[dbo].[SP-Site] ST ON ST.site_sin = J.[sin] JOIN [SecurityPlatform].[dbo].[SP-Client] C ON C.client_id = ST.client_id JOIN [SecurityPlatform].[dbo].[SP-ScheduleCall] SC ON SC.shift_id = S.shift_id JOIN [SecurityPlatform].[dbo].[SP-TrackingCall] TC ON TC.call_id = SC.call_id WHERE S.[status] = 'Confirmed' AND SC.[status] != 'Unconfirmed' AND pin > 0 ORDER BY S.shift_id DESC, SC.win_end DESC"

2 个答案:

答案 0 :(得分:0)

$query = "SELECT TOP 500 start,SC.status,S.shift_id,SC.call_id,SC.type,SC.win_end,S.job_id,pin = CASE 
            WHEN pin IS NULL THEN pin_agency 
            ELSE pin 
        END
        ,CONVERT(VARCHAR, start, 3)+' '+CONVERT(CHAR(5), start, 108) AS start_time
        ,CONVERT(CHAR(5), [end], 108) AS finish_time
        ,guard_name = CASE guard_info
            WHEN 'Agency' THEN  (SELECT shortname FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency)
            ELSE (SELECT shortname FROM [SecurityPlatform].[dbo].[SP-Guard] WHERE pin = S.pin)
        END
        ,guard_number = CASE guard_info
            WHEN 'Agency' THEN  (SELECT phone1 FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency)
            ELSE (SELECT phone_number FROM [SecurityPlatform].[dbo].[SP-Guard] WHERE pin = S.pin)
        END
        ,agency_number = CASE S.guard_info
            WHEN 'Agency' THEN (
                SELECT contact_no_1 FROM [SecurityPlatform].[dbo].[SP-Agency] WHERE agency_id = (
                    SELECT agency_id FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency
                )
            )
            ELSE NULL
        END
        ,C.client_name
        ,ST.site_name
        ,ST.phone_number AS site_phone
        ,ST.site_sin
        FROM [SecurityPlatform].[dbo].[SP-Shift] S
        JOIN [SecurityPlatform].[dbo].[SP-Job] J ON J.job_id = S.job_id
        JOIN [SecurityPlatform].[dbo].[SP-Site] ST ON ST.site_sin = J.[sin]
        JOIN [SecurityPlatform].[dbo].[SP-Client] C ON C.client_id = ST.client_id
        JOIN [SecurityPlatform].[dbo].[SP-ScheduleCall] SC ON SC.shift_id = S.shift_id 
        JOIN [SecurityPlatform].[dbo].[SP-TrackingCall] TC ON TC.call_id = SC.call_id
        WHERE S.[status] = 'Confirmed'
        AND SC.[status] != 'Unconfirmed'
        AND pin > 0 ORDER BY S.shift_id DESC, SC.win_end DESC";
        var_dump($query);    

输出此

SELECT TOP 500 start,SC.status,S.shift_id,SC.call_id,SC.type,SC.win_end,S.job_id,pin = CASE WHEN pin IS NULL THEN pin_agency ELSE pin END ,CONVERT(VARCHAR, start, 3)+' '+CONVERT(CHAR(5), start, 108) AS start_time ,CONVERT(CHAR(5), [end], 108) AS finish_time ,guard_name = CASE guard_info WHEN 'Agency' THEN (SELECT shortname FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency) ELSE (SELECT shortname FROM [SecurityPlatform].[dbo].[SP-Guard] WHERE pin = S.pin) END ,guard_number = CASE guard_info WHEN 'Agency' THEN (SELECT phone1 FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency) ELSE (SELECT phone_number FROM [SecurityPlatform].[dbo].[SP-Guard] WHERE pin = S.pin) END ,agency_number = CASE S.guard_info WHEN 'Agency' THEN ( SELECT contact_no_1 FROM [SecurityPlatform].[dbo].[SP-Agency] WHERE agency_id = ( SELECT agency_id FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency ) ) ELSE NULL END ,C.client_name ,ST.site_name ,ST.phone_number AS site_phone ,ST.site_sin FROM [SecurityPlatform].[dbo].[SP-Shift] S JOIN [SecurityPlatform].[dbo].[SP-Job] J ON J.job_id = S.job_id JOIN [SecurityPlatform].[dbo].[SP-Site] ST ON ST.site_sin = J.[sin] JOIN [SecurityPlatform].[dbo].[SP-Client] C ON C.client_id = ST.client_id JOIN [SecurityPlatform].[dbo].[SP-ScheduleCall] SC ON SC.shift_id = S.shift_id JOIN [SecurityPlatform].[dbo].[SP-TrackingCall] TC ON TC.call_id = SC.call_id WHERE S.[status] = 'Confirmed' AND SC.[status] != 'Unconfirmed' AND pin > 0 ORDER BY S.shift_id DESC, SC.win_end DESC    

对我来说。您可以直接在SQL中尝试var_dump输出吗?我想一点点线索...

答案 1 :(得分:0)

我在这里尝试了所有建议,但是找不到结果,所以我所做的是我将Sql查询更改为Codeigniter的查询生成器,现在它可以正常工作而没有任何时间错误。

希望这对其他人有帮助。

$this->db->select("
    start,
    SC.status
    ,S.shift_id
    ,SC.call_id
    ,SC.type
    ,SC.win_end
    ,S.job_id
    ,pin =
    CASE 
        WHEN pin IS NULL THEN pin_agency 
        ELSE pin 
    END 
    ,CONVERT(VARCHAR, start, 3)+' '+CONVERT(CHAR(5), start, 108) AS start_time
    ,CONVERT(CHAR(5), [end], 108) AS finish_time
    ,guard_name = 
    CASE guard_info
        WHEN 'Agency' THEN  (SELECT shortname FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency)
        ELSE (SELECT shortname FROM [SecurityPlatform].[dbo].[SP-Guard] WHERE pin = S.pin)
    END
    ,guard_number =
    CASE guard_info
        WHEN 'Agency' THEN  (SELECT phone1 FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency)
        ELSE (SELECT phone_number FROM [SecurityPlatform].[dbo].[SP-Guard] WHERE pin = S.pin)
    END
    ,agency_number =
    CASE S.guard_info
        WHEN 'Agency' THEN (
            SELECT contact_no_1 FROM [SecurityPlatform].[dbo].[SP-Agency] WHERE agency_id = (
                SELECT agency_id FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency
            )
        )
        ELSE NULL
    END
    ,C.client_name
    ,ST.site_name
    ,ST.phone_number AS site_phone
    ,ST.site_sin ", FALSE);

    $this->db->from('SP-Shift S');
    $this->db->join('SP-Job J', 'J.job_id = S.job_id');
    $this->db->join('SP-Site ST', 'ST.site_sin = J.sin');
    $this->db->join('SP-Client C', 'C.client_id = ST.client_id');
    $this->db->join('SP-ScheduleCall SC', 'SC.shift_id = S.shift_id');
    $this->db->join('SP-TrackingCall TC', 'TC.call_id = SC.call_id');
    $this->db->where('S.status', 'Confirmed');
    $this->db->where('SC.status != ', 'Unconfirmed');
    $this->db->where('pin >= ', 0);