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

时间:2012-07-26 18:53:47

标签: php sql

我有一个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

有人有线索吗?

1 个答案:

答案 0 :(得分:2)

您无法将表传递给in,但您可以传递子查询:

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