选择零子元素的行(加入)

时间:2015-06-30 13:12:49

标签: sql abap

我有两张桌子。这两个表之间存在约束。

Table1 : Table2 = 1 : n

我想选择Table1中没有条目的Table2元素的键。

示例:

|    Table 1         |
|Key |Col1 |Col2 |...|
|0001|.... |.... |...|
|0002|.... |.... |...|
|0003|.... |.... |...|
|0004|.... |.... |...|

|    Table 2         |
|Tab1|Col1 |Col2 |...|
|0001|.... |.... |...|
|0001|.... |.... |...|
|0003|.... |.... |...|
|0002|.... |.... |...|

Desired output:
0004

输出是table1中唯一没有在table2中输入子项的条目。

我尝试了很多不同的选项,如:

SELECT 
    Key
    FROM Table1
    WHERE 
        ( SELECT COUNT(*) FROM Table2 
            WHERE Table1~Key = Table2~Tab1 
            GROUP BY Table2~Tab1 )

SELECT COUNT(Table2)
    Key
    FROM Table1
    INNER JOIN Table2 
        ON Table1~Key = Table2~Tab1.

...

但它们都不起作用。 有没有人得到 ABAP 的工作查询?

4 个答案:

答案 0 :(得分:2)

 string query1 = "INSERT INTO _1Agent (DealID,ContactPerson,Address,TaxVatNo, 
                     Comm, WorkTel, Cell, Fax, Email,Notes,CreateDate)
                     VALUES ( @DealID , @ContactPerson,@Address ,@TaxVatNo ,
                     @Comm,@WorkTel , @Cell,@Fax,@Email,@Notes,@CreateDate)";

答案 1 :(得分:2)

尝试此查询

select key from table1 where not EXISTS (SELECT *
              FROM table2
              WHERE table1.key=table2.tab1);

答案 2 :(得分:1)

Try following,

SELECT Key FROM Table1
WHERE key not in (SELECT distinct tab1 FROM Table2)

答案 3 :(得分:0)

没有子选择的解决方案:

SELECT t1~Key FROM Table1 as t1
LEFT JOIN Table2 as t2
WHERE t2~Key is null