在 php 和 MySQL 中匹配并返回行

发布于 2024-12-28 07:29:22 字数 821 浏览 0 评论 0原文

我很想得到一些帮助。我正在使用 php 和 MySQL 来构建一个网站。我目前有 3 个表,我将在示例中包含更少的表。基本上我有一个用户表、一个组表和一个组链接表。我拥有的是用户表中的 uid。

我应该如何在 php 中处理它,这样我就可以,比方说:将 users-uid 与 grouplink-uid 匹配,获取与之匹配的 grouplink-gid,将 grouplink-gid 与 groups-gid 匹配并返回 groups-grpname ?并进行 while 循环,以便显示与用户关联的所有组名称。

预先感谢那些愿意伸出援助之手的人。

用户

-------
| uid |
-------
|  1  |
-------  

---------------
| gid |grpname|
---------------
|  1  | grp1  |
---------------
|  2  | grp2  |
---------------

grouplink

-------------------
| glid| uid | gid |
-------------------
|  1  |  1  |  1  |
-------------------
|  2  |  1  |  2  |
-------------------

uid 是用户中的 uid 的 fk,而 gid 是组中的 gid 的 fk

I would love to get some help with this. I'm using php and MySQL to build a website. I currently have 3 tables, I'll include less in the examples. Basically I have a users table, a groups table and a grouplink table. What I have is the uid from the users table.

How should I go about it in php so I could, let's say: match users-uid to grouplink-uid, get the grouplink-gid it matches with, match grouplink-gid to groups-gid and return groups-grpname? And goes on a while loop so all group names the user is associated with are displayed.

Thanks in advance to those who will be willing to extend a hand.

users

-------
| uid |
-------
|  1  |
-------  

groups

---------------
| gid |grpname|
---------------
|  1  | grp1  |
---------------
|  2  | grp2  |
---------------

grouplink

-------------------
| glid| uid | gid |
-------------------
|  1  |  1  |  1  |
-------------------
|  2  |  1  |  2  |
-------------------

uid is fk to uid in users while gid is fk to gid in groups

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

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

发布评论

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

评论(4

七颜 2025-01-04 07:29:22

这只是一个简单的 2 路联接查询:

SELECT users.uid, groups.gid, groups.grpname
FROM users
INNER JOIN grouplink ON users.uid = grouplink.uid
INNER JOIN groups ON grouplink.gid = groups.gid

联接查询结果的实际检索与单个表查询没有什么不同 - 您只是需要处理更多字段。

That's just a simple 2-way join query:

SELECT users.uid, groups.gid, groups.grpname
FROM users
INNER JOIN grouplink ON users.uid = grouplink.uid
INNER JOIN groups ON grouplink.gid = groups.gid

the actual retrieval of a joined query result is no different than a single table query - you've just got more fields to deal with.

最偏执的依靠 2025-01-04 07:29:22

将为您提供所需内容的 SQL 查询如下所示(假设 grouplink 表中没有空值):

SELECT u.uid, g.gid, g.grpname
FROM users u
JOIN grouplink gl ON u.uid = gl.uid
JOIN groups g ON gl.gid = g.gid

The SQL query that will get you what you're looking for goes something like this (assuming no null values in the grouplink table):

SELECT u.uid, g.gid, g.grpname
FROM users u
JOIN grouplink gl ON u.uid = gl.uid
JOIN groups g ON gl.gid = g.gid
离笑几人歌 2025-01-04 07:29:22

这是一种方法:

SELECT users.uid, groups.gid, groups.grpname
FROM users u, groups g, grouplink gl
WHERE g.id = gl.gid
AND gl.uid = u.uid

Here is one way:

SELECT users.uid, groups.gid, groups.grpname
FROM users u, groups g, grouplink gl
WHERE g.id = gl.gid
AND gl.uid = u.uid
风为裳 2025-01-04 07:29:22

当用户 ID 位于变量 $iUserId 中时,您可以查询以下 sql 字符串:

$sSql = "SELECT groups.`grpname` FROM groups
    INNER JOIN grouplink ON groups.`gid` = grouplink.`gid`
    WHERE grouplink.`uid` = '" . intval($iUserId) . "'";
$rRes = mysql_query($sSql);
$aGroups = array();
while (($aRow = mysql_fetch_array($rRes)) !== false) {
   $aGroups[] = $aRow['grpname'];
}

现在与用户关联的所有组都在数组 $aGroups 中。

When the user-id is in the variable $iUserId you could query following sql string:

$sSql = "SELECT groups.`grpname` FROM groups
    INNER JOIN grouplink ON groups.`gid` = grouplink.`gid`
    WHERE grouplink.`uid` = '" . intval($iUserId) . "'";
$rRes = mysql_query($sSql);
$aGroups = array();
while (($aRow = mysql_fetch_array($rRes)) !== false) {
   $aGroups[] = $aRow['grpname'];
}

Now all groups associated with the user are in the array $aGroups.

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