开发者

SQL Query - PgSQL

Have a sql query to construct. Seems pretty basic and I don't seem to be able to wrap my head around it. There are two tables something like this:

Table A : call_id  receive_id        Table B : entity_id   parent
          -------  ----------                  ---------   ------
            x         y                          x           a
            y         z                          y           b
            p         z                          z           b
                                                 p           c

The elements in both call_id column and receive_id column are of type entity. Table B holds the parents for each entity.

I need a query to select only those rows from Table A where the parents of call and receive are not same. In the example table I want all rows except the s开发者_Python百科econd, because y and z have a common parent b.


Try this(having been verified on my MySQL):


select a.* from
A a inner join B b1 on a.call_id=b1.entity_id
    inner join B b2 on a.receive_id=b2.entity_id
where b1.parent<>b2.parent


SELECT *
FROM A
WHERE (SELECT B.parent FROM B WHERE B.entity_id = A.call_id) 
   != (SELECT B.parent FROM B WHERE B.entity_id = A.receive_id)
;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜