怎么写WHERE没有

时间:2011-10-28 17:44:28

标签: php mysql

我正在尝试编写一个查询,它将从“batch”表中选择“batchinstructor”列,其中batchname =“$ batchname”,而“batchid”的值不应该是“$ batchid”。

我设法写了以下内容,但显然是错的,请你帮我纠正一下吗?

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

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

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&lt;&gt; 1'实际上将返回false,从而返回batchid为NULL的记录。如果您有NULL记录,则需要将查询更改为:

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

答案 2 :(得分:2)

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

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

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