不在mysql

时间:2017-10-06 09:22:26

标签: php mysql

我有两个表项目和部门我希望项目表中的项目所有者不在department表的department head字段中 我有代码

select p.* from project p inner join department d on p.department_string_id=d.department_string_id WHERE p.project_owner NOT IN d.department_head

但在我的查询中显示语法错误。我做错了。为什么'NOT IN'子句不起作用。

5 个答案:

答案 0 :(得分:1)

当您想要检查多个值时,

NOT IN将起作用(在()中以逗号分隔)。要匹配单个记录,您可以使用!=<>

select p.* from project p inner join department d on 
p.department_string_id=d.department_string_id WHERE p.project_owner != 
d.department_head

答案 1 :(得分:1)

NOT IN用于检查变量是否存在于值集合中(例如:p.project_owner NOT IN (1,2,3,..)NOT IN (SELECT d.department_head FROM department d)

在这种情况下,您应该使用!=而不是NOT IN,因为您要逐个检查值。

select p.* 
from project p 
inner join department d on p.department_string_id = d.department_string_id 
WHERE p.project_owner != d.department_head

答案 2 :(得分:0)

使用此查询我认为您将实现您想要的目标

select p.* from project p 
left join department d on 
p.department_string_id=d.department_string_id 
WHERE p.project_owner is null

btw innot in未以这种方式使用。它从集合中获取值。

答案 3 :(得分:0)

针对一组元素的<>测试而不仅仅是单个元素!=。根据您想要实现的目标,您的问题有两种解决方案:

  1. 如果您需要针对一个值进行测试,则可以使用department_headNOT IN (select field_name form table) 运算符以排除单个值。

  2. 如果您希望排除整个 select p.* from project p inner join department d on p.department_string_id=d.department_string_id WHERE p.project_owner NOT IN (select d.department_head from department d)列表,则可以使用其他查询来获取列表timestamp。像这样:
  3. date { match => [ "timestamp", "dd/MMM/yyyy:HH:mm:ss Z" ] }

答案 4 :(得分:0)

使用此查询

select p.* from project p 
    left join department d on 
    p.department_string_id=d.department_string_id 
    WHERE d.department_head is null or d.department_head<>p.project_owner