MySQL查询没有给出预期结果!=不工作

时间:2012-12-07 12:45:03

标签: mysql database

我有以下查询,但它没有给出结果。有两个表,visitSearchEngine和clubUsers。我想列出不在clubUsers中的用户。但是使用!=并没有帮助我。它向我展示了clubUsers表中的用户。

 SELECT distinct(vs.users), vs.avisitedDate FROM visitSearchEngine vs, clubUsers cu  
 WHERE vs.users!=''
 AND (vs.users!=cu.users)
 AND vs.status='0' AND vs.hitType='googleIdentified'
 AND ((time_to_Sec(timediff(now(),vs.avisitedDate)))/60)>=5
 AND ((time_to_Sec(timediff(now(),vs.avisitedDate)))/60)<5

请告知我出错的地方,并建议是否有另一种写这个查询的好方法。

由于

3 个答案:

答案 0 :(得分:1)

试试这个:

SELECT DISTINCT(vs.users), vs.avisitedDate FROM visitSearchEngine vs, 
WHERE vs.users!='' AND NOT EXISTS (SELECT * FROM clubUsers cu  WHERE vs.users = cu.users )
AND vs.status='0' AND vs.hitType='googleIdentified'
AND ((TIME_TO_SEC(TIMEDIFF(NOW(),vs.avisitedDate)))/60)>=5
AND ((TIME_TO_SEC(TIMEDIFF(NOW(),vs.avisitedDate)))/60)<5

答案 1 :(得分:1)

试试这个:

 SELECT distinct(vs.users), vs.avisitedDate
 FROM visitSearchEngine as vs LEFT JOIN clubUsers as cu ON vs.users= cu.users
 WHERE vs.status='0' AND vs.hitType='googleIdentified'
 AND ((time_to_Sec(timediff(now(),vs.avisitedDate)))/60)>=5
 AND ((time_to_Sec(timediff(now(),vs.avisitedDate)))/60)<5
 AND cu.users IS NULL;

答案 2 :(得分:0)

您可能需要IS NOT NULL运算符:

vs.users IS NOT NULL

您无法与字符串运算符NULL进行比较:

mysql> SELECT NULL='', NULL<>'';
+---------+----------+
| NULL='' | NULL<>'' |
+---------+----------+
|    NULL |     NULL |
+---------+----------+
1 row in set (0.01 sec)

我还建议让您的查询更容易阅读:

  1. 删除多余的括号
  2. 使用明确的JOIN s