How to write WHERE not


How to write WHERE not

我正在尝试编写一个查询,该查询将从表"batch"中选择列"batchCoacher",其中batchname="$batchname","batchid"的值不应为"$batchid"。

我已经写了以下内容,但显然是错误的,请你帮我纠正一下好吗?

SELECT batchinstructor 
FROM batch 
WHERE batchname='$batchname AND batchid is not= '$batchid'
SELECT batchinstructor 
FROM batch 
WHERE batchname='$batchname' and  batchid <> '$batchid'
SELECT batchinstructor
  FROM batch
 WHERE batchname='$batchname and  batchid <> '$batchid'

这是标准SQL。一些SQL方言会使用

SELECT batchinstructor
  FROM batch
 WHERE batchname='$batchname and  batchid != '$batchid'
SELECT batchinstructor 
FROM batch 
WHERE batchname='$batchname' and batchid <> '$batchid'

SELECT batchinstructor 
FROM batch 
WHERE batchname='$batchname' and batchid != '$batchid'

请注意,如果表中的batchid为NULL,这将不起作用。为此,您确实需要使用IS NOT NULL进行检查,因为检查'NULL<>1'实际上将返回false,从而返回batchid为NULL的记录。如果得到NULL记录,则需要将查询更改为:

SELECT batchinstructor 
FROM batch 
WHERE 
  batchname='$batchname' and 
  (batchid is null or batchid != '$batchid')