简体   繁体   English

SQL查询-PgSQL

[英]SQL Query - PgSQL

Have a sql query to construct. 有一个SQL查询来构造。 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. call_id列和receive_id列中的元素均为实体类型。 Table B holds the parents for each entity. 表B包含每个实体的父母。
I need a query to select only those rows from Table A where the parents of call and receive are not same. 我需要查询以仅从表A中选择那些呼叫和接收的父代不相同的行。 In the example table I want all rows except the second, because y and z have a common parent b. 在示例表中,我想要除第二行之外的所有行,因为y和z具有公共父级b。

Try this(having been verified on my MySQL): 试试这个(已经在我的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<&gtb2.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)
;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM