0

My goal is to return the admins list only if the requester's username is registered as admin / member in that channel.

I store the admins and members list in TABLE channel_member.

CREATE TABLE channel_member (
    id INTEGER NOT NULL,
    uuid VARCHAR(255) NOT NULL,
    user VARCHAR(255) NOT NULL,
    role VARCHAR(255) NOT NULL, // '0' = admin, '1' = member
    PRIMARY KEY (id)
);

What I've tried:

  1. This SELECT statement return multiple row regardless whether the username exists as admin or member.
  @Select("SELECT user_detail.user, user_detail.name " +
          "FROM user_detail " +
          "JOIN channel_member ON user_detail.user=channel_member.user " +
          "AND channel_member.uuid=#{channelUuid} AND channel_member.role='0'")
  List<MemberModel> getChannelAdmins(String username, String channelUuid);

What I am thinking what the logic would look like but don't know how to write it into a SELECT statement:

  1. Get the TABLE user_detail
  2. Get the TABLE channel_member
  3. JOIN table user_detail and channel_member / get intersection that:
    1. channel_member.uuid=#{channelUuid}
    2. channel_member_role='0' // for admin.
  4. (I don't know how) return the result only if channel_member.user=#{username} found in one of the row.

TL:DR, I will post the data down below and the expected result

Table user_detail

id | user       | name
1  | admin      | admin client
2  | member     | member client
3  | member-2   | member 2 client
4  | non-member | non member client

Table channel_member

id | uuid | user     | role
1  | 1    | admin    | 0
2  | 1    | member   | 1
3  | 1    | member-2 | 1
4  | 2    | admin    | 0

The query is (username, channelUuid) e.g (member-2, 1) to get the Admins list.

My expected result is

[
  {
    user: "admin",
    name: "admin client",
  },
]

Another query is (non-member, 1)

My expected result is

[]
Jason Rich Darmawan
  • 1,607
  • 3
  • 14
  • 31
  • 1
    Post sample data and expected results to clarify what you want. – forpas Feb 16 '21 at 14:47
  • @forpas Hi, I've added the query that I would use to clarify it better. I hope that helps. – Jason Rich Darmawan Feb 16 '21 at 14:57
  • Your query takes only 1 parameter: `#{channelUuid}`, so how do you pass user: `'member-2'`? – forpas Feb 16 '21 at 15:20
  • @forpas the current SELECT SQL statement I gave above did not care about the `username` / in other words, everyone can get the `channel detail` regardless whether the user is a member or not. I wish to change that without the need to send 3 query `1. check whether the user is a member / admin in that channel` and then `2. query to get the list of admin of that channel` and then `3. query to get the list of member of that channel`. – Jason Rich Darmawan Feb 16 '21 at 15:24
  • @forpas I am trying to do it step by step, right now I am trying to solve the problem where `everyone can get the admin list of a channel, although he is not a member` – Jason Rich Darmawan Feb 16 '21 at 15:24
  • Table relationship not clear to me. Why is user field in both tables? What is purpose of uuid field - foreign key? Are there only two roles? – June7 Feb 16 '21 at 23:41
  • Apply filter criteria to return admin records but I think whether or not to present output to user must be a separate determination. Perhaps MySQL stored procedure coding could have an IIf() or Case to first determine requestor status and based on that result, run appropriate SELECT. – June7 Feb 16 '21 at 23:56
  • Does this answer your question? [Run a query in a MySQL stored procedure if a condition is true](https://stackoverflow.com/questions/9845171/run-a-query-in-a-mysql-stored-procedure-if-a-condition-is-true) – June7 Feb 16 '21 at 23:56

0 Answers0