多个SQL查询条件

时间:2019-02-05 07:30:59

标签: sql ms-access ms-access-2016

是否可以在Access查询中应用以下条件?

联盟查询未提供全部三个条件

LName, FName 
Where (TourID=14XXX and Status=0) AND 
      (TourID=16XXX and Status=1) AND 
      (TourID=20XXX and Status=1)

仅返回所有三个条件都为真的结果

2 个答案:

答案 0 :(得分:0)

使用OR代替AND

select LName, FName from tablename
Where (TourID=14XXX and Status=0) OR
      (TourID=16XXX and Status=1) OR
      (TourID=20XXX and Status=1)

答案 1 :(得分:0)

我认为您想要汇总:

SELECT LName, FName 
FROM t
WHERE (TourID=14XXX and Status=0) OR 
      (TourID=16XXX and Status=1) OR 
      (TourID=20XXX and Status=1)
GROUP BY LName, FName 
HAVING COUNT(*) = 3;

COUNT(*)假定原始表中没有重复的游览。

SELECT LName, FName 
FROM t
WHERE (TourID=14XXX and Status=0) OR 
      (TourID=16XXX and Status=1) OR 
      (TourID=20XXX and Status=1)
GROUP BY LName, FName 
HAVING SUM(IIF(TourID=14XXX AND Status=0, 1, 0)) > 0 AND
       SUM(IIF(TourID=16XXX and Status=1, 1, 0)) > 0 AND
       SUM(IIF(TourID=20XXX and Status=1, 1, 0)) > 0;