在mysql查询中使用NOT IN的正确方法


The correct way to use NOT IN in a mysql query

我有一个mysql查询,它抛出了一个错误,我猜这是因为我明智地使用了短语"NOT IN":

$sqlGetCountry = mysqli_query($link, "SELECT * FROM locations WHERE country='$country' AND CURTIME() > time AND '$state' NOT IN state ORDER BY time desc LIMIT 20");
$sqlNumCountry = mysqli_num_rows($sqlGetCountry);

我有一张包含城市、州和国家的表格,我基本上是在试图查找结果中没有给定州(在本例中为$state,可能是德克萨斯州、夏威夷州等)的查询。我得到错误:

mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given

有人知道线索吗?

不能将表传递给in,但可以传递子查询:

SELECT  * 
FROM    locations 
WHERE   country='$country' 
        AND CURTIME() > time 
        AND '$state' NOT IN (select state from state)
ORDER BY 
       time desc 
LIMIT  20