如何排除同时满足两个条件的记录?

时间:2018-12-20 16:11:35

标签: mysql

我有一个相当复杂的查询

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'

这里是小提琴 http://sqlfiddle.com/#!9/737e61/1

1 个答案:

答案 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