我有一张“汽车”表,上面有一列颜色。我正在尝试构建一个查询,该查询将显示所有有 4 个门的汽车,但排除所有在其颜色列中记录有“红色”或“黑色”的汽车。
SELECT * FROM 'cars'
WHERE 'cars.doors = 4'
AND 'cars.colour != red OR black'
这似乎不起作用,但我不确定是不是因为我的代码中是否有其他东西搞砸了,所以我弄错了。这样做可以吗?
谢谢
您将条件用单引号括起来,这是无效的。您应该只用引号将字符串括起来。对于非红色或黑色部分,您可以使用NOT IN()
SELECT * FROM `cars`
WHERE cars.doors = 4
AND cars.colour NOT IN('red', 'black')
或者你可以做很长的路,注意使用AND
notOR
因为OR
不适合你想要的。
AND cars.colour != 'red' AND cars.colour != 'black'
您可以使用NOT IN
引号,也不正确地使用backtics
` 作为列和表名。
SELECT * FROM `cars`
WHERE `cars`.`doors` = 4
AND `cars`.`colour` NOT IN ('red','black')
您必须明确告知哪些字段必须进行比较
SELECT * FROM `cars`
WHERE `cars`.´doors` = 4
AND `cars`.´colour` != 'red' AND `cars`.´colour` != 'black'
你的报价也有问题。我已经纠正了他们
在您的where'cars.colour != red OR black'
条件下,您错误地放置了
这应该是
SELECT * FROM cars
WHERE doors = 4
AND colour != red AND colour != black
或者
SELECT * FROM cars
WHERE doors = 4
AND colour NOT IN('red', 'black')
尝试
SELECT * FROM `cars`
WHERE cars.doors = 4
AND cars.colour NOT IN ("red","black");
SELECT * FROM cars WHERE (cars.doors = 4) AND ( cars.colour NOT IN ("red","black"))
以及其他解决方案:
SELECT * FROM cars WHERE (cars.door = 4) AND (cars.colour <> "red") AND (cars.colour <> "black")