我有两个包含多列的数据集,我想根据两列验证它们具有相同的条目,如下所示:
表1
a | b
-----
1 | 1
1 | 3
2 | 3
2 | 4
表2
a | b
-----
1 | 2
1 | 3
2 | 3
2 | 7
我希望两个人知道表1中的所有元组都没有出现在表2中。
输出结果为:
a | b
-----
1 | 1
2 | 4
它似乎并不复杂,但我找不到解决方案或关于它的帖子。
答案 0 :(得分:2)
您没有指定DBMS,因此这是ANSI标准SQL。
$.ajax({
type: 'POST',
url: '/Routing/GetRoutings',
dataType: 'Json',
data: { PID: $(this).val() },
success: function (data) {
data = JSON.parse(data);
$('#DOPT tbody').empty();
$.each(data, function (index, data) {
$('#DOPT').append('<tr><td>' + data.Operation_Number + ' - ' + data.Operation_Description + '</td></tr>');
});
$('#DOP').show();
$('#NWO').show();
},
error: function (ex) {
}
});
select col1, col2
from table_1
except
select col1, col2
from table_2;
返回表1中未出现在表2中的所有行
答案 1 :(得分:1)
使用MINUS的替代方案
SELECT t1.col1, t1.col2 FROM t1
MINUS
SELECT t2.col1, t2.col2 FROM t2
所有SQL数据库都不支持MINUS运算符。它可以在Oracle等数据库中使用。对于SQL Server,PostgreSQL和SQLite等数据库,请使用EXCEPT运算符执行此类查询。
答案 2 :(得分:0)
两种不同的方法:
使用ANTI-Join
Person.__init__(self,first,last,age)
Person.__str__(self)
使用相关查询而不存在
SELECT T1.*
FROM table1 T1
LEFT JOIN table2 T2
on T1.Col1 = T2.col1
and T1.Col2 = T2.Col2
WHERE T2.Col1 is null
或者您可以使用完全外部联接来查找不在Either中的记录(前提是RDBMS支持完全外部联接)
SELECT T1.*
FROM table1 T1
WHERE NOT EXISTS (SELECT *
FROM table2 T2
WHERE T1.Col1 = T2.col1
and T1.Col2 = T2.Col2)
或者如果您只想要两列并且不关心哪个表具有哪些记录,则您的选择可以是
SELECT T1.*, T2.*
FROM table1 T1
FULL OUTER JOIN table2 T2
on T1.Col1 = T2.col1
and T1.Col2 = T2.Col2
WHERE T2.Col1 is null or T1.Col1 is null
答案 3 :(得分:0)
如果您指出正在使用的SQL版本并添加了一些列名称会很有帮助,但您可以尝试这样的事情:
it('should call for the someMethod', () => {
spyOn(ComponentClass.prototype, 'someMethod');
// instantiate ComponentClass class
expect(component.someMethod).toHaveBeenCalled();
});