How do i get relationships between users? - mysql

I have a table called users, that looks like this for example:
Table: users
username id
Simon 6
Ida 7
And a relationships table
Table: Relationships
me partner
6 7
Now for every relationship that is created the me and partner columns will get the id depending on which one of the users sent the request.
If Ida sends a request to Simon, the column looks like this: me: 7 partner: 6
because Ida is id number 7 and Simon is number 6.
But if Simon sends a request then the columns looks like this: me: 6 partner: 7
What i want to do is to write a query that will get the right id from the relationships table for every user with a relationship, and then use the id to Join the users table and get the partners username and print it out.
My problem is that the columns Me and Partner can have different values depending on which of the 2 users sended the request first.
How do i write a query that prints out the right information for each and every user and give them the right id of their partner?
How the output should look like:
From simons point of view*
: You are in a relationship with Ida.
From Idas point of view *
: You are in a relationship with Simon.

I want to offer an alternative.
Join the user table to the relationship table either on the me column or the partner column.
Then join the user table back onto the relationship table using the same criteria.
SELECT u.username,
FROM users u
JOIN relationships r ON u.id = r.me OR u.id = r.partner
JOIN users u2 ON r.me = u2.id OR r.partner = u2.id
WHERE u.id = 'THE USER YOU CARE ABOUT'
Untested. If you throw up some CREATE TABLEs and INSERT INTOs we can all have a play.

If you want to get all users which have a relationship with some user (ex.: id:2) you specify, I would use something like this:
SELECT id, username
FROM user,
(SELECT receiver_id as rel_id FROM relationship WHERE sender_id=2 union
SELECT sender_id as rel_id FROM relationship WHERE receiver_id=2) tab
WHERE id=rel_id
The inner statement means you are selecting all the relationships in which 2 was the sender and all the relationships in which 2 was the receiver and stacking them in the same column (union).
After that, the table user is used to get the user name from the id.

Select username as username,
(select username name from users uSubq where uSubq.id = r.partner) as partnername
from users u join relationships r on u.id=r.me
UNION
Select username as username,
(select username name from users uSubq where uSubq.id = r.me) as partnername
from users u join relationships r on u.id=r.partner;
Here is the test for the query: Sql Fiddle
If either Ida sent request to Simon or the reverse but not both, use UNION ALL for better performance.

Related

How to show username if i have 2 tables, user and review

i am creating a restaurant review website. in my review table i have a foreign key called user_id and idk how to use it to display the username which is in the user table
my user table
my review table
so my question is how do i display the username from this? what mysql statement do i have to write. I am lost on what to do
Assuming you want to try and get the review text along with the user name from the corresponding user you can use a join to combine the info for example:
SELECT u.username, r.review_text
FROM reviews r
LEFT JOIN users u
ON (u.user_id = r.user_id)
I assumed the users table is called users and reviews table is called reviews but update those as necessary each is "aliased" as u and r respectively and then tables are joined
If the relationship between the two tables is mapped out correctly you should be able to run a query to fetch the name of each user. Try to avoid any N+1 query though

how to select two tables which has inner joints and union all tables

hello I am new to php I am having 2 tables I want to select 2 columns which matches accesstoken and user id column from 2 tables
for eg...I had joined (users table and sessions table ) (admin table and adminsessions) now I want to check data either one of these 2 combinations eg....if (users.id=sessions.userid) or (admin.id =adminseesions.userid) I am passing accesstoken in url and I want to match userid and accesstoken is it possible to archive this task with union all because these 2 pairs are independent and I am expecting output as follow
if my header has userid which is in adminsessions but not in sessions tabel then I want to display the data which is in adminsessions
or if userid from header which matches data in sessions then I want to display following details
here I want to fetch any single row from above tables based on userid
following are the 2 tables which I want to select one of the table where accesstoken column and userid matches
following are my user and admin table where I want to match one of the id column in two tables
Yes, that's possible using UNION.
SELECT * FROM (
SELECT userid, accesstoken, refreshtoken
FROM adminsession
UNION ALL
SELECT userid, accesstoken, refreshtoken
FROM sessions) all_sessions
WHERE userid = :somegivenuserid;
This query will return all session information by :somegivenuserid, regardless of whether this information was found in table adminsession or sessions. In the same way you can get the user data, just that you use tables users and admin.
More information can be found in section UNION Clause of MySQL's documentation.
Update
The OP looked for
SELECT userid, accesstokenexpiry
FROM adminsession INNER JOIN admin
ON adminsession.userid = admin.id
AND accesstoken = :accesstoken
UNION ALL
SELECT userid, accesstokenexpiry
FROM sessions INNER JOIN users
ON sessions.userid = users.id
AND accesstoken = :accesstokenusr

How to select from two tables with IF?

I have 2 table in my DB, first is users, and second is friends.
In the users tables I have a column named `friend' this columns have a random ID of each one, and this ID you used for calling someone to subscribe with your URL.
but if an old person want to become your friend I add other table named friends
content the random ID + ID_USER
now, I want to SELECT * FROM users, friends WHERE users.friends = 'BlaBla' OR friends.RANDOMID = 'BlaBla'
I try with this code, but I doesn't work, it's give all persons having the random
in calling friend
SELECT *
FROM users, friends
WHERE users.Role = "User"
AND users.call = :random
OR (users.ID = friends.ID_USER
AND friends.random = :random)
Any solution?

A request to get a Twitter feed like?

How does Twitter (or Facebook) do to retrieve a user feed?
Here is the database schema I am working on. I renamed everything to look like twitter hoping that the most of you would understand my question...
I only have two resources: users and tweets. So here are those two tables:
users:
- id
- username
tweets:
- id
- author_id
- content
Let's continue with a simple pivot table to associate tweets and users:
user_tweet:
- user_id
- tweet_id
- created_at
This pivot table is here mainly to store the retweets. But it also stores the original tweets for more convenience.
For example, let's take user 1 tweets 'something'. user 2 and user 3 retweet. The user_tweet table will have three rows.
And, let's see a last table that complicates everything: The Following System.
Every user can follow an other user. I named the table "followee_follower":
followee_follower:
- followee_id
- follower_id
The followee_id is the user.id of the person being followed
The follower_id is the user.id of the person that follows an other one
Now let's get to the SQL problem:
I'm user 1. I follow user 2, and user 3.
How can I retrieve the tweets and retweets from user 2 and user3 knowing that I want to retrieve them ordering them by the created_at field of the user_tweet table, and that I don't want to get two similar tweets.
Many thanks, any help is highly nice from you,
Have a good day/night.
EDIT: Here are some samples data from tables:
users table
tweets table
user_tweet table
followee_follower table
expected results
I'm not sure I completely understand your question (sample data would help), but I think you just need to use multiple joins:
select t.id, t.content, ut.created_at
from tweets t
inner join user_tweets ut on t.id = ut.tweet_id
inner join followee_follower ff on ut.user_id = ff.follower_id
where ff.followee_id = 1
order by ut.created_at
Perhaps if a user retweets, you'd want to do something like this instead to get the first tweet (assuming the id and created_at fields both should return the minimum):
select t.content, min(t.id), min(ut.created_at)
from tweets t
inner join user_tweets ut on t.id = ut.tweet_id
inner join followee_follower ff on ut.user_id = ff.follower_id
where ff.followee_id = 1
group by t.content
order by min(ut.created_at)

how to find a user available online by checking two tables?

I have 2 tables 'Users' and 'Friends':
Table: Users(username,email,location,online_status)
Table: Friends(Friend_rq_from,Friend_request_to,status)
'Users' table is for number of registered users in a website
and 'Friends' table for individual users for maintaing thier friends..
i want to count eg: mithun's friends from 'Friends' table(1 indicating they are friends in Friends table) who are online in Users table..(1 indicate online in 'users' table)
its a little bit difficult for me because, 'Friends' table have 2 fields which 'mithun' can be in 'Friend_rq_from' field or 'Friend_request_to' field...
how to find this?
NB: Friend_rq_from: Indicating who start a friend request,
Friend_request_to: Indicating to whom a request sent(or who is waiting to accept)
SELECT COUNT(distinct u.username)
FROM Users u
INNER JOIN Friends f
ON (u.username = f.Friend_rq_from AND f.Friend_rq_to='mithun' AND f.status=1)
OR (u.username = f.Friend_rq_to AND f.Friend_rq_from='mithun' AND f.status=1)
WHERE u.online_status = 1 AND u.username <> 'mithun'