在多对多关系中从查找表分配值

发布于 2024-12-27 20:13:35 字数 555 浏览 0 评论 0原文

我有下表:

Persons(
PersonID,
FirstName,
LastNAme)

Person_Categories(
CategoryID,
CategoryName)

Persons_PersonCategories(
PersonID,
CategoryID)

一个人可以有多个类别,很多人可以属于一个类别。 我希望能够从下拉菜单(Person_Categories)中为人员分配类别。我编写了以下查询,如果已经为人员分配了类别,则该查询会显示数据。

SELECT Persons_PersonCategories.PrsCatID, PrsCategory
FROM Person_Categories
INNER JOIN Persons_PersonCategories
ON Persons_PersonCategories.PrsCatID = Person_Categories.PrsCatID;

我的问题:我需要什么sql语句将Person_Categories表中的类别分配给Persons?

非常感谢,赞

I have the following tables:

Persons(
PersonID,
FirstName,
LastNAme)

Person_Categories(
CategoryID,
CategoryName)

Persons_PersonCategories(
PersonID,
CategoryID)

One person can have more than one category and many people can belong to one category.
I want to be able to assign categories to persons from a drop down menu (Person_Categories). I've written the following query which displays data if there are already categories assigned to persons.

SELECT Persons_PersonCategories.PrsCatID, PrsCategory
FROM Person_Categories
INNER JOIN Persons_PersonCategories
ON Persons_PersonCategories.PrsCatID = Person_Categories.PrsCatID;

My question: What sql statement do I need to assign categories from the Person_Categories table to Persons?

Many thanks, zan

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

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

发布评论

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

评论(1

不一样的天空 2025-01-03 20:13:35

当用户从菜单列表中选择类别时,您可以将其分配给人员:

INSERT INTO Persons_PersonCategories (PersonID, CategoryID)
VALUES (<person ID>, <category ID>);

其中 <人员 ID><类别 ID> 分别代表人员您要分配的类别以及用户选择的类别。

要从给定人员中删除类别,您可以使用:

DELETE FROM Persons_PersonCategories
WHERE PersonID   = <person ID>
  AND CategoryID = <category ID>;

其中 <人员 ID><类别 ID> 分别代表要从中删除类别的人员和用户正在删除的类别。

希望这有帮助。

When the user selects a category from the menu list, you can assign it to a person with:

INSERT INTO Persons_PersonCategories (PersonID, CategoryID)
VALUES (<person ID>, <category ID>);

where <person ID> and <category ID> respectively represent the person you are assigning to and the category the user has choosen.

To remove a category from a given person, you can use:

DELETE FROM Persons_PersonCategories
WHERE PersonID   = <person ID>
  AND CategoryID = <category ID>;

where <person ID> and <category ID> respectively represent the person you are removing categories from and the category the user is removing.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文