如何表现!=或<>在postgresql中

时间:2017-05-12 11:02:47

标签: postgresql

我需要获取table2中不存在的table1内容我不认为<>或!=运营商,所以任何人都可以帮助我

select t1._id, t1."categoryName", t1.company_id, t1.active from table1 t1
inner join table2 t2 on t1._id <> t2.category_id 
inner join table3 t3 on t2 .department_id <> t3 ._id where  t3._id = 1

例如: 一个拼贴画可以有10个部门,学生注册到5个部门,其中有1个拼贴画 table1是dept 表3是col 表2看起来像这样:

col_id| dept_id   | student_id
1     |    1      |     1
1     |    2      |     2
1     |    3      |     3
1     |    4      |     4
1     |    5      |     5

我需要获取表2中不存在的dept_id的其余部分

3 个答案:

答案 0 :(得分:2)

  

我需要获取table2中不存在的table1内容

使用not exists查询,如下所示:

select * 
from table1 t1
where not exists (select *
                  from table2 t2
                  where t1._id = t2.category_id)

请注意table3如何与问题&#34相关;不存在于表2和#34;

答案 1 :(得分:0)

尝试不进入或外部联接,就像这里:

select t1._id, t1."categoryName", t1.company_id, t1.active 
from table1 t1
left outer join table2 t2 on t1._id = t2.category_id 
left outer join table3 t3 on t2 .department_id = t3 ._id 
where  t3._id = 1
and t2.category_id is null
and t3 ._id is null

答案 2 :(得分:0)

是的,我从dbastackexchange得到了我的问题的解决方案,这可能会帮助其他人如此粘贴

select
    cat._id,
    cat.cat_name
from
    cat
where
    not exists(
        select
            *
        from
            deptcat
            inner join dept on(
                dept._id = deptcat.dept_id
            )
        where
            deptcat.cat_id = cat._id
            and dept._id = 1
    )