How to join 2 entities based on NOT condition

前端 未结 1 1386
长情又很酷
长情又很酷 2021-01-14 07:14

I have 2 tables - TableA and TableB for example with some data in them as defined in this post - How to join results of 2 tables based on not condition

Now I am plan

相关标签:
1条回答
  • You need to use a theta-style join:

    select b, a
    from TableB b, TableA a 
    where not exists (
        select 1
        from TableB b1, TableA a1
        where 
            b1.partNumber = a1.partNumber and
            b1.id = b.id and
            a1.id = a.id    
    )   
    order by b.id
    

    or you can use an SQL query to fetch entities as well:

    List result = session.createSQLQuery("SELECT b.*, c.* \n" +
            "FROM TableB b AS t\n" +
            "CROSS JOIN (SELECT id AS Aid, name AS Aname, partNumber AS Apart\n" +
            "            FROM TableA AS a\n" +
            "            WHERE NOT EXISTS (SELECT 1\n" +
            "                              FROM TableB AS b\n" +
            "                              WHERE b.partNumber = a.partNumber)) AS c\n" +
            "ORDER BY b.id ")
            .addEntity("b", B.class)
            .addEntity("a", A.class)
            .list();
    
    0 讨论(0)
提交回复
热议问题