Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 选择字段与另一行相同的行_Sql_Postgresql - Fatal编程技术网

Sql 选择字段与另一行相同的行

Sql 选择字段与另一行相同的行,sql,postgresql,Sql,Postgresql,我有两张表:产品和帖子。产品是一种消费品,例如iPhone X,发布是一种在线市场上的产品列表,例如易趣发布。单个产品具有零个或多个关联的过帐 有没有办法只选择有兄弟姐妹的帖子?即,选择产品栏等于任何其他过帐产品栏的所有过帐 从发布a中选择* a.product=b.id上的内部联接产品b 其中COUNTb>0 我想知道你的内部连接应该只做的把戏,但如果我错过了什么,你可以试试这个 With a as ( SELECT a.*,b.*, count(*) over(Partition by b

我有两张表:产品和帖子。产品是一种消费品,例如iPhone X,发布是一种在线市场上的产品列表,例如易趣发布。单个产品具有零个或多个关联的过帐

有没有办法只选择有兄弟姐妹的帖子?即,选择产品栏等于任何其他过帐产品栏的所有过帐

从发布a中选择* a.product=b.id上的内部联接产品b 其中COUNTb>0
我想知道你的内部连接应该只做的把戏,但如果我错过了什么,你可以试试这个

With a as 
(
SELECT a.*,b.*, count(*) over(Partition by b.id) cnt 
FROM postings a
INNER JOIN products b ON a.product = b.id
)
Select * from a where cnt > 0
如果你只是想发帖,那么我建议:

select p.*
from postings p
where exists (select 1
              from postings p2
              where p2.product = p.product and
                    p2.posting_id <> p.posting_id
             );

请注意,这些不需要products表,因为产品信息可以在过帐中找到。

任何列表都可以没有与之关联的产品吗?列表是否也有唯一的ID列?使用当前查询时会发生什么情况?另外,如果你想要非唯一的,你可能会想让你的约束>1,因为这将导致两个或更多的同一行。这就是我正在寻找的功能!SQL从不令人失望。我很感激你的帮助,这帮我节省了很多时间。
select p.*
from (select p.*,
             count(*) over (partition by p.product) as cnt
      from postings p
     ) p
where cnt > 1;