如何仅显示我是会员的已加入组(php)?

时间:2019-05-09 06:44:24

标签: mysql

表格

用户:

enter image description here

组:

enter image description here

示例:

| groupID | name    | createdBy | description | groupIdentity |
---------------------------------------------------------------
|    1    | test    | user2     | etst        | 4455          |
---------------------------------------------------------------
|    2    | another | user2     | etst        | 4220          |
---------------------------------------------------------------
|    3    | new     | user1     | test        | 4221          |
---------------------------------------------------------------  

group_members:

enter image description here

示例:

| groupID | memberID | userStatus | groupIdentity |
---------------------------------------------------
| 1       | user1    |   1        | 4455          |
---------------------------------------------------
| 2       | user1    |   0        | 4220          |
---------------------------------------------------
| 3       | user2    |   1        | 4221          |
---------------------------------------------------

userStatus值:

  • 0 =请求正在等待组
  • 1 =用户是的成员 群组
  • 2 =用户是该组的主持人
  • 3 =的创建者 小组

即使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>
      ";
   }

输出错误,因为我已向“另一个”组发送了请求

The Wrong output

正确的输出将是: enter image description here

2 个答案:

答案 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中的保留关键字

see mysql doc