简体   繁体   中英

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. 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.

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.

关系表

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

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:

 {
  "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. 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)

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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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