简体   繁体   English

选择待处理请求(Friendship Restful Api)PHP和MySql

[英]Select pending requests (Friendship Restful Api) PHP and MySql

I want to develop a social networking application for Android where users could send requests for friendship and accepting the same. 我想为Android开发一个社交网络应用程序,用户可以发送友情请求并接受友情请求。 I have achieve that. 我已经做到了。 Users can send requests to others like this: 用户可以向其他人发送请求,如下所示:

Users table (users) 用户表(用户)

id                                               
name
email
...

Relationship table (relationship) 关系表(关系)

user_one_id - id of user who is sending the request 
user_two_id - id of user who is receiving request
status - 0 for pending request, 1 for accepting request and so on..
action_user_id - which user has done some action in table

But i'm not getting the exact number of pending requests when i use select query. 但是当我使用select查询时,我没有得到确切的待处理请求数。

And now i will show you an example where user with ID 1 is sending requests and users with ID 2 and ID 4 are receiving request. 现在我将向您展示一个示例,其中ID为1的用户正在发送请求,而ID为2且ID为4的用户正在接收请求。

关系表

Now if i try to get pending requests using this query: 现在,如果我尝试使用此查询获取待处理的请求:

"SELECT u.name, r.user_one_id, r.user_two_id, r.status FROM users u, relationship r WHERE r.user_one_id = ? OR r.user_two_id = ? AND r.status = 0 AND u.id = r.user_one_id AND r.action_user_id != ?"

And parameters that are required are: user_one_id , user_two_id and action_user_id 所需的参数是: user_one_iduser_two_idaction_user_id

In this example user with ID 1 is logged in and he shouldn't be able to see any pending requests because he didn't receive none, but if i send this query from above with parameters 1 for user_one_id , 1 for user_two_id and 1 for action_user_id , i'm getting JSON response like this: 在此示例中,ID为1的用户已登录,并且他不应该看到任何待处理的请求,因为他没有收到任何请求,但是如果我从上面发送此查询,其中参数1表示user_one_id1表示user_two_id1表示action_user_id ,我得到这样的JSON响应:

 {
  "error": false,
  "requests": [
    {
      "name": "Dusan" // user with id 1
    },
    {
      "name": "Dusan"
    },
    {
      "name": "Jelena Radenkovic" // user with id 2
    },
    {
      "name": "Jelena Radenkovic"
    },
    {
      "name": "Stefan Dimitrijevic" // user with id 4
    },
    {
      "name": "Stefan Dimitrijevic"
    }
  ]
}

but this is perfectly works if user didn't send any request, like users with ID 2 and ID 4. If i send the same query with 2 for parameters, i'm getting then only one record as it should be. 但是如果用户没有发送任何请求,例如ID为2和ID 4的用户,这是完全有效的。如果我发送带有2的相同查询参数,那么我只得到一条记录。 This is what i'm getting: 这就是我得到的:

    {
  "error": false,
  "requests": [
    {
      "name": "Dusan" // getting only this user with id 1 because he sent request to the user with id 2
    }
  ]
}

This is okay until i send request to other users.. I hope that i have explained everything well and clearly. 这是好的,直到我向其他用户发送请求..我希望我已经很清楚地解释了一切。 But if it's not clear, let me know, i will explained more or better. 但如果不清楚,请告诉我,我会解释得更多或更好。 Thanks in advance. 提前致谢。

As per your schema user_one_id is sender id and user_two_id is receiver id, so to get exact pending request for current user(ie logged in user) your query will be as follow: (no need to use action_user_id in this case) 根据您的架构, user_one_id是发件人ID, user_two_id是接收者ID,因此要获得当前用户(即登录用户)的确切待处理请求,您的查询将如下所示:(在这种情况下无需使用action_user_id)

SELECT u.name, r.user_one_id, r.user_two_id, r.status FROM users u, relationship r WHERE r.user_two_id = <current_user_id> AND r.status = 0 AND u.id = r.user_one_id

or you can use join also in this case: 或者你也可以在这种情况下使用join:

SELECT u.name, r.user_one_id, r.user_two_id, r.status FROM relationship r join users u on u.id = r.user_one_id WHERE r.user_two_id = <current_user_id> AND r.status = 0

Now if you want to get how many request sent by current user then change user_one_id to user_two_id as follow: 现在,如果您想获取当前用户发送的请求user_one_iduser_two_id更改为user_two_id ,如下所示:

SELECT u.name, r.user_one_id, r.user_two_id, r.status FROM users u, relationship r WHERE r.user_one_id = <current_user_id> AND r.status = 0 AND u.id = r.user_two_id

and same changes in join also. 加入时也有相同的变化。

You can also try this: 你也可以试试这个:

SELECT s.user_one_id,us.name,us.email
FROM relationship as s
INNER JOIN user AS us ON s.user_one_id = s.id
WHERE s.user_two_id = "'.$current_user.'" AND s.status = 0

在r.user_two_id = u.id上尝试此查询SELECT COUNT(u.user_two_id)作为来自关系r内部联接用户的friend_request_count,其中status = 0且r.user_two_id = your_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM