我有一个相当复杂的查询
SELECT g.guidUser,u1.username,g.sent,g.accepted,g.declined,g.rewarded,g.guidUserVersus,u2.username AS usernameUserVersus
FROM games g
INNER JOIN userdata u1 ON g.guidUser=u1.guidUser
INNER JOIN userdata u2 ON g.guidUserVersus=u2.guidUser
WHERE
((g.sent!="" OR g.accepted!="" OR g.declined!="")
AND g.rewarded="" AND (g.guidUser='1' OR g.guidUserVersus='1'))
ORDER BY g.guidUser, g.idGame
现在我要排除同时具有的记录
(g.accepted!=“” AND g.guidUserVersus ='1')
我尝试过
SELECT g.guidUser,u1.username,g.sent,g.accepted,g.declined,g.rewarded,g.guidUserVersus,u2.username AS usernameUserVersus,g.idGameVersus,g.timeFinal
FROM games g
INNER JOIN userdata u1 ON g.guidUser=u1.guidUser
INNER JOIN userdata u2 ON g.guidUserVersus=u2.guidUser
WHERE
((g.sent!="" OR g.accepted!="" OR g.declined!="")
AND g.rewarded="" AND (g.guidUser='1' OR g.guidUserVersus='1'))
** AND !(g.accepted!="" AND g.guidUserVersus='1') **
ORDER BY g.guidUser, g.idGame
但是似乎没有选择它应该的记录,例如具有g.accepted!=“” AND g.guidUserVersus ='2'
答案 0 :(得分:2)
编辑:您需要在条件内用AND替换AND。
SELECT g.guidUser,u1.username,g.sent,g.accepted,g.declined,g.rewarded,g.guidUserVersus,u2.username AS usernameUserVersus,g.idGameVersus,g.timeFinal
FROM games g
INNER JOIN userdata u1 ON g.guidUser=u1.guidUser
INNER JOIN userdata u2 ON g.guidUserVersus=u2.guidUser
WHERE
((g.sent!="" OR g.accepted!="" OR g.declined!="")
AND g.rewarded="" AND (g.guidUser='1' OR g.guidUserVersus='1'))
AND NOT (g.accepted!="" OR g.guidUserVersus='1')
ORDER BY g.guidUser, g.idGame