Sql 在我的案例中,如何比较表中的记录?
我有两张这样的桌子 表Sql 在我的案例中,如何比较表中的记录?,sql,postgresql,Sql,Postgresql,我有两张这样的桌子 表 id | name | type | 1 test 1 parent 2 test 3 parent 3 test 3 child 4 test 4 parent 5 test 5 child 6 test 6 child 表B child_id | parent_id 6 1 5 2 4 3 基本上,我有两个表有
id | name | type |
1 test 1 parent
2 test 3 parent
3 test 3 child
4 test 4 parent
5 test 5 child
6 test 6 child
表B
child_id | parent_id
6 1
5 2
4 3
基本上,我有两个表有错误的记录,我想通过查询找到它们
我需要找到应该是父项但类型为子项的记录。(id:3
在我的例子中)
及
查找应为子项但类型为父项的记录。(id:4
在本例中)
我尝试了以下查询
select * from TabelB as b left join TableA as a on b.child_id = a.id WHERE a.type=parent
他们只会给我显示作为父项的类型,但我找不到坏记录
在这种情况下,我需要找到test3
和test4
记录。我不知道如何进行这项工作。有人能帮我吗?非常感谢
select *
from
a
inner join
b on (a.id, a.type) in ((b.child_id, 'parent'), (b.parent_id, 'child'))