不知道如何得到一定的结果

时间:2019-05-08 16:56:09

标签: sql postgresql

我有这种表结构。

fn |    property    |                              value                              
----+----------------+-----------------------------------------------------------------
  1 | File_Real_Path | /Users/Shared/catalog
 19 | File_Real_Path | /Users/Shared/catalog/123.mov
  4 | File_Real_Path | /Users/Shared/catalog/230230230.mp4
 21 | File_Real_Path | /Users/Shared/catalog/4142.mov
 20 | File_Real_Path | /Users/Shared/catalog/5959.mp4
 22 | File_Real_Path | /Users/Shared/catalog/999.mp4
  3 | File_Real_Path | /Users/Shared/catalog/Job Interview   Good Example Copeeeee.mp4
 18 | File_Real_Path | /Users/Shared/catalog/Screen Shot 2019-03-22 at 13.01.46.png
  8 | File_Real_Path | /Users/Shared/catalog/aaall.mp4
  7 | File_Real_Path | /Users/Shared/catalog/aasww.mp4
  5 | File_Real_Path | /Users/Shared/catalog/awww.mp4
  6 | File_Real_Path | /Users/Shared/catalog/brum.mp4
  2 | File_Real_Path | /Users/Shared/catalog/test.mov
 19 | filename       | /Users/Shared/catalog/123333.mov

我想通过过滤仅具有名称的属性(文件名和File_Real_Path)来获取甚至不具有相同id的值。输出应该是这样的。

fn |    property    |              value               
----+----------------+----------------------------------
 19 | File_Real_Path | /Users/Shared/catalog/123.mov
 19 | filename       | /Users/Shared/catalog/123333.mov

这是我尝试的查询,但是即使具有-filename的属性不存在-

,它也会返回File_Real_Path的所有行
select e1.fn, e1.property, e1.value
from metadatas  e1
inner join metadatas as e2
on e1.fn = e2.fn
and e1.property != e2.property
and e1.value != e2.value
where e1.property in ('filename','File_Real_Path')
group by e1.fn, e1.property, e1.value
order by e1.property, e1.value;

希望您了解我想说的话。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您要寻找exists吗?

select m.*
from metadatas m
where m.property in ('filename', 'File_Real_Path') and
      exists (select 1
              from metadatas2 m2
              where m2.property in ('filename','File_Real_Path') and
                    m2.fn = m.fn and
                    m2.value <> m.value and m2.property <> m.property
             );