如何在SQL查询中添加NOT子句

时间:2017-10-04 08:46:29

标签: mysql sql

我想在这个SQL查询中添加一个NOT子句:

Select 
    members.Member_Id, 
    members.Title, 
    members.FirstName, 
    members.LastName, 
    members.Po_Box, 
    members.Street, 
    members.City, 
    members.Del, 
    members.Mobile, 
    members.eMail, 
    members.WFTD, 
    ship_info.Renewal_Date
From 
    members 
Left Join
    ship_info on (members.Member_Id = ship_info.Member_Id)
Order By
    ship_info.Renewal_Date

NOT子句是这样的:

Where Member_Id Not Between 2000 And 3000;

我曾尝试将此行放在不同的位置,但每次都会出错,而且由于Wamp报告法语错误,因此发现它无益。

是的,有类似的问题,但是他们让我更加困惑,因为我不太了解它们,因此无法修改我的脚本。我对数组和复杂的代码知之甚少。

我从数据库导出数据以用于邮件合并目的,而Ids在2000年代的成员是已删除的成员,其Id已从原始ID移动到更高的数字,因为我不喜欢永久删除人员他们后来改变了主意。

谢谢。

4 个答案:

答案 0 :(得分:2)

你可以这样试试:

Select members.Member_Id
  ,members.Title
  ,members.FirstName
  ,members.LastName
  ,members.Po_Box
  ,members.Street
  ,members.City
  ,members.Del
  ,members.Mobile
  ,members.eMail
  ,members.WFTD
  ,ship_info.Renewal_Date
From members 
LEFT JOIN ship_info ON (members.Member_Id=ship_info.Member_Id)

WHERE (members.Member_ID < 2000 OR members.member_ID > 3000)

ORDER BY ship_info.Renewal_Date

答案 1 :(得分:1)

由于您正在使用JOIN并且member_id字段驻留在JOIN的两个表中,因此您必须指定需要where语句的表。尝试:

SELECT members.Member_Id
    ,members.Title
    ,members.FirstName
    ,members.LastName
    ,members.Po_Box
    ,members.Street
    ,members.City
    ,members.Del
    ,members.Mobile
    ,members.eMail
    ,members.WFTD
    ,ship_info.Renewal_Date
FROM members
LEFT JOIN ship_info ON (members.Member_Id = ship_info.Member_Id)
WHERE members.Member_Id NOT BETWEEN 2000
        AND 3000
ORDER BY ship_info.Renewal_Date

答案 2 :(得分:1)

根据&#34;数据库管理基础&#34;,以下是适当的语法:

Select members.Member_Id
  ,members.Title
  ,members.FirstName
  ,members.LastName
  ,members.Po_Box
  ,members.Street
  ,members.City
  ,members.Del
  ,members.Mobile
  ,members.eMail
  ,members.WFTD
  ,ship_info.Renewal_Date
From members 
LEFT JOIN ship_info ON (members.Member_Id=ship_info.Member_Id)

WHERE NOT members.Member_Id BETWEEN 2000 AND 3000

ORDER BY ship_info.Renewal_Date ;

在前面提到的书中,它指出,

  

&#34;可以在 FROM WHERE 中指定 JOIN 语句   子句,但建议您在 FROM 子句中指定它们   &#34;

至于子句优先级,这是正确的顺序:

SELECT     -- "what" we want
FROM       -- "where" to look for it
JOIN
WHERE      -- condition 
GROUP BY  
HAVING
ORDER BY   -- sorting order

答案 3 :(得分:0)

我认为下面应该有用,如果没有那么请指出你得到的错误,你只需要在表名members前加上(如果你把这个表名用作别名,那就更好了)

SELECT members.Member_Id
    ,members.Title
    ,members.FirstName
    ,members.LastName
    ,members.Po_Box
    ,members.Street
    ,members.City
    ,members.Del
    ,members.Mobile
    ,members.eMail
    ,members.WFTD
    ,ship_info.Renewal_Date
FROM members
LEFT JOIN ship_info ON (members.Member_Id = ship_info.Member_Id)
WHERE members.Member_Id NOT BETWEEN 2000
        AND 3000
ORDER BY ship_info.Renewal_Date