使用连接使用条件编写查询

write query using joins using condition

有两个 tables table1,table2.

table1:

number type name
1100 1 steve
1100 2 john
1100 2 jack

....... 等等不同的数字

类似地table2:

number type name
1100 1 abraham
1100 2 john
1100 2 jack

等等不同的数字... 预期 output:join 两个 table 基于相同的数量和相同的类型,但在名称条件下它应该打印上面有 mismatch.for 示例的地方 john had type '2' in table 1 和 table 2 中也有 john,但对于类型“1”,它不匹配,因为 steve <> abraham.I 正在执行此查询,但输出也包含带有 jack 和 john 的行:

number type name1 name2
1100 1 steve abraham
1100 2 john jack
1100 2 jack john

此处 table 1 和 table 2 中的行数相等,每个 table 中每种类型的行数也相等

select * from table1,table2 where table1.number=table2.number and table1.type=table2.type and table1.name <> table2.name

预期输出:

number type name1 name2
1100 1 steve abraham

正如你在上面看到的,我已经给出了我为查询得到的输出(它包含 jack,John 对,但我不想要它们,因为类型 '2' 的名称在两者中都是相同的 tables) 但是如果你看到类型 '1' names are different in both tables 所以我想打印 them.if names are different in type'2' for both tables I也希望它们作为输出,但在示例中,类型“2”的 table 中的名称相同。任何人都可以帮助我..?

我想这会做你想要的。

SELECT table1.number
    ,table1.type
    ,table1.name
    ,table2.name
FROM table1
    JOIN table2 ON 
        table2.number = table1.number AND
        table2.type = table1.type AND
        table1.name NOT IN (SELECT t2.name 
                              FROM table2 t2 
                              WHERE t2.number = table1.number AND 
                                t2.type = table1.type)