如果从其他表中的值不存在,则如何插入行?

发布于 2025-02-11 07:00:55 字数 338 浏览 2 评论 0原文

我有三个表:Person,<代码> LEAD 和Legal_entityLegal_entity.personid参考Person.IDlead.personid也参考Person.ID。 我需要做的是将带有Legal_entity.personid的新行插入Lead> Lead表(如果还不存在)。我需要从Legal_entity表的每行进行此行。我不知道该怎么做。

I have three tables: person, lead and legal_entity.
legal_entity.personId references person.Id and lead.personId also references person.Id.
What I need to do is to insert a new row with the value of legal_entity.personId to the lead table if it doesn't exist already. I need to do it for every row from legal_entity table. I have no idea how to do something like this.

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

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

发布评论

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

评论(1

小…楫夜泊 2025-02-18 07:00:55
INSERT INTO lead (person_id)
SELECT p.id
FROM legal_entity le
INNER JOIN person p on le.person_id = p.id
LEFT JOIN lead l on p.id = l.person_id
WHERE l.id IS NULL
INSERT INTO lead (person_id)
SELECT p.id
FROM legal_entity le
INNER JOIN person p on le.person_id = p.id
LEFT JOIN lead l on p.id = l.person_id
WHERE l.id IS NULL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文