如何使用多个表删除sql中的记录

发布于 2024-12-25 07:07:26 字数 342 浏览 1 评论 0原文

我在sql中遇到问题,谁能帮我 我有3张桌子 sublocation、postingdetail 和员工

我必须从 sublocation 表中删除 sublocid 但首先我必须检查是否有任何员工在 postsdetail 表中处理该子位置,如果是,则不应删除该记录。

表子定位

sublocid
子地址名
mainlocid

表 postsdetail

empid
子区域
mainloc

表员工

empid
员工姓名
亚定位
mainlocid

I'm having a problem in sql, can anyone help me
I have 3 tables
sublocation, postingdetail and employee

I have to delete sublocid from sublocation table
but first i have to check that is there any employee working on that sublocation from postingdetail table, if yes then the record shouldn't be deleted.

table sublocation

sublocid
sublocname
mainlocid

table postingdetail

empid
subloc
mainloc

table employee

empid
empname
sublocid
mainlocid

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白鸥掠海 2025-01-01 07:07:26

像这样的东西

DELETE 
FROM [SUBLOCATION] 
LEFT JOIN [EMPLOYEE] ON [SUBLOCATION].[sublocid] = [EMPLOYEE].[sublocid] 
LEFT JOIN [POSTINGDETAIL] ON [POSTINGDETAIL].[empid] = [EMPLOYEE].[empid] 
WHERE empid IS NULL

可能会起作用。

Something like

DELETE 
FROM [SUBLOCATION] 
LEFT JOIN [EMPLOYEE] ON [SUBLOCATION].[sublocid] = [EMPLOYEE].[sublocid] 
LEFT JOIN [POSTINGDETAIL] ON [POSTINGDETAIL].[empid] = [EMPLOYEE].[empid] 
WHERE empid IS NULL

That might work.

油焖大侠 2025-01-01 07:07:26

好吧,我不熟悉 sql-server,我自己是 MySQL 用户,但我知道 sql-server 确实有存储过程...这就是存储过程的用途...

这里有一个关于存储过程的小教程。

Well I am not familiar with sql-server, I am a MySQL user myself, but I know sql-server does have stored procedures... and that's what stored procedures were made to do...

Here is a little tutorial on stored procedures.

十年九夏 2025-01-01 07:07:26

我不明白postingdetail表的含义,也不明白为什么我们从表employee而不是postingdetail中获取sublocid,但是您的查询应该如下所示:

delete sublocation
where sublocid not in
(
    select e.sublocid
    from employee e
    join postingdetail p on e.empid = p.emid
)

I don't understand the meaning of the postingdetail table, and why we are taking the sublocid from table employee rather than postingdetail, but you query should look something like this:

delete sublocation
where sublocid not in
(
    select e.sublocid
    from employee e
    join postingdetail p on e.empid = p.emid
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文