也许我错过了什么。我正在尝试做一个非常简单的选择语句。我想要的逻辑是,如果两列(ActivityDate和CreateDate)彼此相等AND ONE OR THE OTHER我们的列和DoubleBookedHours列为零,则应该返回:
SELECT *
FROM DailyTaskHours
WHERE ActivityDate = CreateDate
AND (
Hours != 0
AND DoubleBookedHours != 0
)
上面没有回复。
上图中的第一行是它应该返回的内容,因为小时和DoubleBookedHours这两列都不是零,但日期是等价的。第二行是我不希望它返回的,因为Hours和DoubleBookedHours都为零。
我做错了什么?
编辑:
SELECT *
FROM DailyTaskHours
WHERE ActivityDate = CreateDate AND Hours != 0 AND DoubleBookedHours != 0
以上不返回任何内容。
SELECT *
FROM DailyTaskHours
WHERE ActivityDate = CreateDate
AND Hours = 0
AND DoubleBookedHours = 0
上面的内容与我想要的相反:
答案 0 :(得分:5)
你表现恰恰相反。通过使用!
,您可以说除了0之外的所有内容。此外,您不需要在where子句中使用括号。
SELECT *
FROM DailyTaskHours
WHERE ActivityDate = CreateDate
AND Hours = 0
AND DoubleBookedHours = 0
如果您希望那些没有Hours = 0
和DubleBookedHours = 0
的人返回,那么:
SELECT *
FROM DailyTaskHours
WHERE ActivityDate = CreateDate
AND NOT
(Hours = 0 AND DoubleBookedHours = 0)
答案 1 :(得分:2)
应该是:
--test data
DECLARE @DailyTaskHours AS TABLE (ActivityDate datetime, CreateDate datetime, [Hours] int, DoubleBookedHours int)
INSERT INTO @DailyTaskHours VALUES ('01 Jan 2012','01 Jan 2012',0,1)
INSERT INTO @DailyTaskHours VALUES ('01 Jan 2012','01 Jan 2012',0,0)
--query
SELECT *
FROM @DailyTaskHours
WHERE ActivityDate = CreateDate
AND ([Hours] <> 0 OR DoubleBookedHours <> 0)
--result
ActivityDate CreateDate Hours DoubleBookedHours
2012-01-01 00:00:00.000 2012-01-01 00:00:00.000 0 1
答案 2 :(得分:2)
SELECT *
FROM DailyTaskHours
WHERE ActivityDate = CreateDate
AND (
Hours != 0
OR -- this
DoubleBookedHours != 0
)
答案 3 :(得分:0)
SELECT *
FROM DailyTaskHours
WHERE ActivityDate = CreateDate
AND Hours = 0
AND DoubleBookedHours = 0