2016-06-10 53 views
0

我正在通過PDO運行查詢。當我在phpMyAdmin控制檯中直接運行查詢時,查詢運行正常,但在通過PDO運行時返回錯誤。它返回行,就好像NOT EXISTS子句不在那裏一樣。我的查詢是有效的這樣的:PDO無法識別NOT EXISTS子句

SELECT ul.session, CONCAT(u.firstname,' ', u.lastname) AS owner, ... 
    FROM usagelog ul 
INNER JOIN companies c ON ul.company=c.companyid 
INNER JOIN users u ON ul.user=u.userid 
WHERE (ul.company=:compid OR 0=:compid) 
    AND ul.created BETWEEN :start AND :end 
    AND NOT EXISTS(SELECT ss.session FROM seshstatechangelog ss WHERE ss.session=ul.session AND ss.stateto != :state) 
ORDER BY ul.created 

我與:的兩個變量?樣式的綁定,並試圖爲你在這裏看到。

有沒有其他人有這個問題?

+1

給'AND(ul.created BETWEEN:start AND:end)'並嘗試。 –

+1

@nana partykar它的工作,謝謝 - 但爲什麼它需要括號? – AntG

+0

我把它寫成我的答案。 @AntG –

回答

1
SELECT ul.session, CONCAT(u.firstname,' ', u.lastname) AS owner, ... 
    FROM usagelog ul 
INNER JOIN companies c ON ul.company=c.companyid 
INNER JOIN users u ON ul.user=u.userid 
WHERE (ul.company=:compid OR 0=:compid) 
    AND (ul.created BETWEEN :start AND :end) 
    AND NOT EXISTS(SELECT ss.session FROM seshstatechangelog ss WHERE ss.session=ul.session AND ss.stateto != :state) 
ORDER BY ul.created 

給,AND (ul.created BETWEEN :start AND :end)。何時,有多少條件。嘗試使用()附上它。可讀性會很容易。可能是AND :endAND NOT EXISTS被視爲一個條件。

使用parenthesis()NOT EXISTS得到的結果,因爲你想一個或另一個,然後使用,並得到了created date範圍,最後一個,並從company id連接的結果。