我正在通过PDO运行查询。当我直接在phpMyAdmin控制台中运行时,查询运行正常,但在通过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
我已尝试使用?
样式绑定变量和:
,如您所见。
还有其他人有这个问题吗?
答案 0 :(得分: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
Give,AND (ul.created BETWEEN :start AND :end)
。什么时候,有很多条件。尝试使用(
和)
将其括起来。它易于阅读。可能是AND :end
,AND NOT EXISTS
被视为一个条件。
使用parenthesis()
从NOT EXISTS
获取结果,因为您需要其中一个,然后使用AND获取created date
范围,最后使用AND来加入结果company id
。