我需要删除目标表中存在但源表中没有的记录。目标表中的主键是源表中不存在的auto_increment ID。源表和目标表都包含一组唯一的键组合,可用于唯一地标识两个表中的行。我应该遵循什么方法?如果我要使用多个列组合作为唯一键而不是一个主键(源代码中不存在),该如何删除?
delete from dest_table
where (uniq_key_col1,uniq_key_col2) not in (
select dest.uniq_key_col1,dest.uniq_key_col2
from dest_table dest
join source_table source
on dest.uniq_key_col1=source.uniq_key_col1
and dest.uniq_key_col2=source.uniq_key_col2
)
这是理想的外观(出于清楚起见而提供,由于多列,请忽略where子句中的错误)
答案 0 :(得分:2)
您可以使用存在。即:
delete from dest_table
where not exists (
select *
from source_table source
where dest_table.uniq_key_col1=source.uniq_key_col1
and dest_table.uniq_key_col2=source.uniq_key_col2
);
答案 1 :(得分:2)
您可以这样:
DELETE
FROM dbo.dest a
WHERE NOT EXISTS (
SELECT 1
FROM dbo.source1 b
WHERE a.id1 = b.ID1 and a.id2 = b.id2
)
答案 2 :(得分:1)
另一个适合您的选择
class App extends Component {
constructor(props) {
super(props);
const { pathname } = this.props.location;
this.state = {
isNavbarHidden: pathname === "/" || pathname === "/Register"
};
}
componentDidMount() {
this.unlisten = this.props.history.listen(location => {
const { pathname } = location;
const isNavbarHidden = pathname === "/" || pathname === "/Register";
this.setState({ isNavbarHidden });
});
}
componentWillUnmount() {
this.unlisten();
}
render() {
const { isNavbarHidden } = this.state;
return (
<div>
{!isNavbarHidden && <Navigation />}
<Link to="/">Login</Link>
<Link to="/Register">Register</Link>
<Link to="/Home">Home</Link>
<Link to="/Dashboard">Dashboard</Link>
<div className="sans-serif">
<Route exact path="/" component={Login} />
<Route exact path="/Register" component={Register} />
<Route exact path="/Home" component={Home} />
<Route exact path="/Dashboard" component={Dashboard} />
</div>
</div>
);
}
}
ReactDOM.render(
<BrowserRouter>
<Route path="/" component={App} />
</BrowserRouter>,
document.getElementById("root")
);
答案 3 :(得分:1)
听起来好像不需要EXISTS
DELETE d FROM dest_table d
WHERE NOT EXISTS (SELECT (PUT_APPROPRIATE_COLUMNS_HERE) from source_table s
WHERE d.col1 = s.col
AND d.col2 = s.col2
... etc for other columns
)
请注意表别名,您需要这样做。如果可能的话,使用内部联接可能更合适。