mySQL表链接,组链接到其他成员列表,显示所有成员

发布于 2024-08-24 15:54:34 字数 1450 浏览 4 评论 0原文

我有一个带有 group(g1)members(m1) 列表的数据库,它本身工作得很好。

不过,我想添加 group(g1) 的功能,将不同的 groups(g2-3) 添加到其列表中,以便查询可以显示所有 结果是 >members(M1)+(m2-3)

我的表是:

Group(1) Table:  Group1, 
Member(2) table: MemberA, MemberB, MemberC etc.

我认为我需要某种类型的链接表,其中 group(1) 输入它想要订阅 Group(2) 成员。

我在想链接表应该是这样的:

GroupID,  Group Name,GroupID, GROUP subscribed to Name 
Group(1), FancyGroup(1), Group(2), shabby Group(2) 

这就是我希望 Group(1) 查询在订阅 Group(2) 后的结果:

Fancy Group
Fancy MemberA
Fancy MemberB
Fancy MemberC
Shabby  MemberA
Shabby Member B

任何有想法吗?我意识到这是一个很长的问题,但我不知道纠正它的更短方法?

更新3/9:

这些是我的表名称:

该组称为family;行是 (userid,loginName..etc)
会员组称为member;行是(memberid、loginName、name、 等)
连接表称为user2member;行是 (userid,memberid) 。

这就是我用于查询的内容:

SELECT member.name
   FROM family
      JOIN user2member on family.userid = member.memberid
      JOIN member on user2member.name = member.name
     WHERE family.userid = '30'
   ORDER BY member.name

我收到此错误:~...syntax to use close 'Â WHERE family.userid = '30' ORDER BY member.name LIMIT 0, 30' at line 5

想法?

I have a database with a group(g1) and a list of members(m1), by itself it works fine.

However I want to add the ability of the group(g1) to add different groups(g2-3) to it's list so that a query would bring up ALL the members(M1)+(m2-3) as a result.

My tables are:

Group(1) Table:  Group1, 
Member(2) table: MemberA, MemberB, MemberC etc.

I think I need some type of linking table, where group(1) inputs that it wants to subscribe to a Group(2) member.

I was thinking the linking table would look like:

GroupID,  Group Name,GroupID, GROUP subscribed to Name 
Group(1), FancyGroup(1), Group(2), shabby Group(2) 

This is what I want the results for Group(1) query to look like after it subscribe to Group(2):

Fancy Group
Fancy MemberA
Fancy MemberB
Fancy MemberC
Shabby  MemberA
Shabby Member B

Any Ideas? I realize this is a long question but I didn't know a shorter way of righting it?

UPDATE 3/9:

These are my table names:

The Group is called family; Rows are (userid,loginName..etc)
The Member group is called member; rows are (memberid,loginName,name, etc)
The Join table is called user2member; rows are (userid,memberid) .

This is what I am using for the query:

SELECT member.name
   FROM family
      JOIN user2member on family.userid = member.memberid
      JOIN member on user2member.name = member.name
     WHERE family.userid = '30'
   ORDER BY member.name

I'm gettin this error:~...syntax to use near 'Â WHERE family.userid = '30' ORDER BY member.name LIMIT 0, 30' at line 5

Ideas?

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

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

发布评论

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

评论(1

自此以后,行同陌路 2024-08-31 15:54:34

我认为你试图代表团体和成员之间的多对多关系。

为此,您需要一个组表,每个组对应一行。
GroupID、GroupName,无论什么,无论什么。

您需要一个成员表,其中每个人一行。
会员 ID、名字、姓氏,等等。

然后,您需要一个连接表、一个组成员资格表。最简单的组成员资格表具有像这样的行

MemberID, GroupID

每个组的每个成员都有一行。如果您的应用程序需要,您可以向此表添加其他内容,例如 DateJoined 或 ActiveMembership 等。

然后,您可以使用联接来取回数据。如果您想要“极客”组中的成员列表,您可以使用这样的联接。

SELECT m.Firstname, m.Lastname
  FROM group g
  JOIN groupmembership gm on g.GroupID = gm.GroupID
  JOIN member m on gm.MemberID = m.MemberID
 WHERE g.GroupName = 'geeks'
ORDER BY m.Lastname, m.Firstname

如果您想要不属于任何组的成员列表,您可以这样做。

SELECT m.Firstname, m.Lastname
  FROM member m
  LEFT JOIN groupmembership gm on m.MemberID = gm.MemberID
 WHERE gm.GroupID IS NULL
ORDER BY m.Lastname, m.Firstname

这是一个超级有用的设计模式。祝你好运。

I think you're trying to represent a many-to-many relationship between groups and members.

To do this, you need a group table with a row for each group.
GroupID, GroupName, whatever, whatever.

You need a member table with a row for each person.
MemberID, Firstname, Lastname, whatever, whatever.

You then need a join table, a groupmembership table. The simplest group membership table has rows like this

MemberID, GroupID

It has one row per member per group. You could add other things to this table if your application needed them, such as DateJoined or ActiveMembership, or whatever.

Then, you'd use joins to get your data back. If you wanted a list of members in the "geeks" group, you'd use a join like this.

SELECT m.Firstname, m.Lastname
  FROM group g
  JOIN groupmembership gm on g.GroupID = gm.GroupID
  JOIN member m on gm.MemberID = m.MemberID
 WHERE g.GroupName = 'geeks'
ORDER BY m.Lastname, m.Firstname

If you wanted a list of members not belonging to any group, you'd do this.

SELECT m.Firstname, m.Lastname
  FROM member m
  LEFT JOIN groupmembership gm on m.MemberID = gm.MemberID
 WHERE gm.GroupID IS NULL
ORDER BY m.Lastname, m.Firstname

It's a super-useful design pattern. Good luck.

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