表格
用户:
组:
示例:
| groupID | name | createdBy | description | groupIdentity |
---------------------------------------------------------------
| 1 | test | user2 | etst | 4455 |
---------------------------------------------------------------
| 2 | another | user2 | etst | 4220 |
---------------------------------------------------------------
| 3 | new | user1 | test | 4221 |
---------------------------------------------------------------
group_members:
示例:
| groupID | memberID | userStatus | groupIdentity |
---------------------------------------------------
| 1 | user1 | 1 | 4455 |
---------------------------------------------------
| 2 | user1 | 0 | 4220 |
---------------------------------------------------
| 3 | user2 | 1 | 4221 |
---------------------------------------------------
userStatus
值:
即使userStatus
等于1或2但不等于0和3,我仍想显示所有为我创建的为特定用户创建的组而没有我自己的组。
我可以使该工作不显示自己创建的组。
我当前的查询:
$currentUser = $_SESSION['name'];
$sql = "SELECT DISTINCT *
FROM group_members INNER JOIN `group` ON group.groupID = group_members.groupID
WHERE createdBy <> '$currentUser'";
也尝试过
$currentUser = $_SESSION['name'];
$sql = "SELECT DISTINCT *
FROM group_members INNER JOIN `group` ON group.groupID = group_members.groupID
WHERE createdBy <> '$currentUser' AND userStatus <> '0'";
但它仍然显示该群组以及我发送加入请求的位置。
还尝试了if
语句:
if ($row['userStatus'] >= '1') {
$groupID = $row['groupIdentity'];
$groupTitles = $row['name'];
echo "
<a href='?key=$groupID'>
<div class='mini-card my-cards'>
<span class='groupBtnsWrapper'>
$notifications
</span>
<div class='div2'>
$groupTitles
</div>
</div>
</a>
";
}
输出错误,因为我已向“另一个”组发送了请求
答案 0 :(得分:1)
尝试一下。让我知道它是否有效。我使用表名grp
代替了group
,因为它是保留关键字,应避免使用。
假设您想要用户已加入的组名,并且状态为1或2。
select A.name from grp A, group_members B
where A.groupIdentity = B.groupIdentity
AND B.groupIdentity IN (
select groupIdentity from grp_mem
where userStatus IN (1, 2)
and memberID == "user1"
)
php代码:
$currentUser = $_SESSION['name'];
$sql = "select A.name from grp A, group_members B
where A.groupIdentity = B.groupIdentity
AND B.groupIdentity IN (
select groupIdentity from grp_mem
where userStatus IN (1, 2)
and memberID == '$currentUser'
)";
答案 1 :(得分:-1)
SELECT * FROM group_members INNER JOIN `group` ON group.groupID = group_members.groupID
WHERE createdBy <> '$currentUser' AND userStatus <> '0' GROUP BY group.groupID
注意将表名组更改为其他名称,这是mysql中的保留关键字