PHP SQL join relations [duplicate] - php

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Self referencing relation with followers|friends users
Hi there.
I have two tables called users and users_friends.
users:
id INT
username VARCHAR
.....
users_friends:
user_id INT
friend_id INT
PRIMARY KEY (`user_id`,`friend_id`)
How can I get the information about a specific users friends? Let's say I want to list friends associated with the user with an id of 5.
Can't get my joins working, everything I try ends up with no result. Any ideas? This should be I simple query with two INNER JOIN I think but can't manage.
Then a second question is how do I structure this using Kohana 3.1 ORM? Perhaps I shouldn't?
EDIT: Anyone have an idea about doing this with Kohana 3 ORM?

What about something like this :
select users.*
from users
inner join users_friends on users_friends.friend_id = users.user_id
where users_friends.user_id = 5

SELECT *
FROM `users` AS u
INNER JOIN `users_friends` AS uf ON u.id = uf.user_id
WHERE u.id = '5'
this is the query with INNER JOIN
if all of the information is stored in users you will need to join users once more
SELECT ufu.*
FROM `users` AS u
INNER JOIN `users_friends` AS uf ON u.id = uf.user_id
INNER JOIN `users` AS ufu ON uf.friend_id = ufu.user_ud
WHERE u.id = '5'

Ofcourse a JOIN is possible, but that wont be faster then a simple query like:
SELECT *
FROM users
WHERE id IN (
SELECT friendID FROM user_friends WHERE userID=5
)
But, this only lists the friends from user 5. If you want the other way around, so list the people that say user 5 is a friend, this query is also easier to alter.
SELECT *
FROM users
WHERE id IN (
SELECT userID FROM user_friends WHERE friendID=5
)

isn't it a simple:
Select friend_id from users_friends where user_id = 5
5 being the users id.

Off the top of my head:
SELECT friend_id, username
FROM users_friends JOIN users
ON users_friends.friend_id = users.id
WHERE user_id = 5;

Related

SQL - join selected data or table for conditions

I have SQL tables for users and relationships, like
users{ID, name, email}
relationships{ID, uid, relid, type}
I want to get all "friends" ID, name and email (by current user ID) by SQL, using JOIN or whatever.
If using join is it better to JOIN the table of users and select relationships or select relationships and join users?
something like this (I invented now, i use a bit diferrent tables and code, sorry for errors)
1:
SELECT DISTINCT u.ID, u.name, u.email FROM users u JOIN relationships r ON (r.uid = 1 OR r.relid = 1) AND r.type = 1 WHERE u.ID = r.uid OR u.ID = r.relid
2:
SELECT DISTINCT u.ID, u.name, u.email FROM relationships r JOIN users u ON r.uid = u.ID OR r.relid = u.ID = 1 WHERE r.uid = u.ID OR r.related = u.ID
Edit:
I use DISTINCT becouse when I select users whoose IDs equals to r.uid or r.related, I also get the current user (uid + related) for every relation. DISTINCT should unique the users? I'm beginner and found this on stackoverflow.
So, is it better to select from users and join relations or select relations and join users?
BTW: I quite like the answer with UNION
You can union the users where user is in the uid and in the relid.
SELECT users.*
FROM users
INNER JOIN relationships AS rel
ON users.ID = rel.uid
WHERE users.ID = 500
UNION
SELECT users.*
FROM users
INNER JOIN relationships AS rel
ON users.ID = rel.relid
WHERE users.ID = 500
Not sure why you are using DISTINCT but it looks like you just want to select all the rows from the relationships table (the friends), and then join their details from the users table.
SELECT u.id, u.name, u.email
FROM relationships r
LEFT JOIN users u ON u.id = r.uid
WHERE r.uid = 12345

Fetching user from friends list and follow list

I am working on social networking site.
I've three tables one is user table which is used to store user details, another table is follow table which is used for followers following list.
In this table I am storing user_id and follower_id.
Third table is user_friends in this I'm storing user_id and friend_userid.
I want to search the user from my friends list and follow list. For this i've written query like this:-
select f.follower_id,uf.friend_userid,u.user_id,u.first_name,u.last_name from tbl_user u
LEFT JOIN tbl_userfriends uf ON uf.friend_userid = u.user_id
LEFT JOIN tbl_follow f ON f.follower_id = u.user_id
where uf.friend_userid != '11'
AND u.first_name LIKE '%a%'
This query returning users only who are friends it is not returning the follow users.
Any help will be appreciated. Thanks in Advance.
You're joining tbl_follow on the Follower ID being equal to the User ID. I suspect that's probably not right.
If you don't already have one you'll need a user id key in the follower table to join on, then you can change your join to;
LEFT JOIN tbl_follow f ON f.userid = u.user_id
i've done this by using following query:-
select u.user_id,u.first_name,u.last_name from tbl_user u LEFT JOIN tbl_userfriends uf ON uf.friend_userid = u.user_id LEFT JOIN tbl_follow f1 ON f1.follower_id = u.user_id LEFT JOIN tbl_follow f2 ON f2.user_id = u.user_id where (uf.user_id = '11' OR f1.user_id = '11' OR f2.follower_id = '11') AND (u.first_name LIKE '%s%' OR u.last_name LIKE '%s%') AND u.status = '0' group by u.first_name
This query returning me all the users who are my followers, friends and to whom i am following.

SQL select from all from table that are not in other table

I'm trying to select all users that their user id is not in another table.
Users table (user id is user_id)
Ghosts table (user id is id)
this is what I got so far:
$sql = "select * from users where users.user_id <> ghosts.id;";
Please advise...
Cheers!
You can use NOT IN:
select * from users u where u.user_id not in (select id from ghosts);
or NOT EXISTS:
select *
from users u
where not exists (select 1 from ghosts g where g.id = u.user_id)
Something like this may also work, although I like Brian's answer.
SELECT *
FROM users u
LEFT JOIN ghosts g
ON u.user_id = g.id
WHERE g.id IS NULL

php friends table

I'm struggling with this for hours so please help me.
This is my users table
id | username | last_activity(timestamp for online system)
And this is my friends table
id | uid | fid
What I want is to order the output by last_activity
My current query looks like this:
SELECT fid FROM friends WHERE uid='$user_id'
SELECT f.fid FROM f.friends
LEFT JOIN users u ON f.uid = u.id
WHERE uid=$user_id
ORDER BY u.last_activity DESC
You want to use INNER JOIN to join the two tables:
SELECT f.fid
FROM friends f
INNER JOIN users u
ON u.id = f.id
ORDER BY u.last_activity DESC
Always make sure you type out a real JOIN clause as some ways are old and getting more and more unsupported.
Read more here:
INNER JOIN on w3schools
I think you want
SELECT f.fid AS fid
FROM friends f, users u
WHERE f.uid = u.id AND f.uid = $user_id
ORDER BY u.last_activity DESC
I assume your problem is that fid is on one table, but the ordering criterion last_activity is on another table. The goal of this query is to JOIN each row in the friends table with the corresponding row in the users table (via the WHERE clause).
Assuming uid in friends table is foreign key references to id in users
table. so using INNER JOIN you can retrieve your desired results.
SELECT f.fid
FROM friends f INNER JOIN user_tb u
ON u.id = f.uid
WHERE f.uid = '$user_id'
ORDER BY u.last_activity DESC;

Friendship System Sql Structure & Query

I am coding a friendship system and it has two tables.
members
id
username
password
friends
id
user_id
friend_id
status
Let's say that i want a query that can select the friends IDs of the member $userId how possible to make this in one query?
I found a solution which is to make 2 queries. The fist selects the friends WHERE user_id = $userId AND the second selects friends WHERE friend_id = $userId and then MIX them in one array. If there is no other solution I'm going to use it.
please any ideas for both the SQL structure & Queries?
Use:
SELECT f.friend_id
FROM FRIENDS f
WHERE f.user_id = $user_id
UNION
SELECT t.user_id
FROM FRIENDS t
WHERE t.friend_id = $user_id
Using UNION will remove duplicates. UNION ALL would be faster, but it doesn't remove duplicates.
If you want to get the information for the friends from the MEMBERS table, use:
SELECT m.*
FROM MEMBERS m
JOIN (SELECT f.friend_id 'user_id'
FROM FRIENDS f
WHERE f.user_id = $user_id
UNION
SELECT t.user_id
FROM FRIENDS t
WHERE t.friend_id = $user_id) x ON x.user_id = m.id
BTW: I hope you're using mysql_escape_string on the variables, otherwise you risk SQL injection attacks:
You should be able to try using
SELECT m.*
FROM friends f INNER JOIN
members m ON f.friend_id = m.user_id
WHERE f.user_id = $userId
This will give you all the Friends details
To get BOTH have a look at
SELECT DISTINCT CASE WHEN f.user_id = $userId then f.friend_id else f.user_id END CASE
FROM friends f
WHERE f.user_id = $userId
OR f.friend_id = $userId
Why not inserting 2 rows for 1 friendship. For example:
Let's say we have 2 user will become friends
User_id : 1 &
Friend_id : 2
insert into friends (user_id, friend_id, status) values (1,2,0)
insert into friends (user_id, friend_id, status) values (2,1,0)
so you can select easily by simple select query.
Also it will ease the pain for your likely next question "How to find Mutual Friends".
Since you're asking for something simple like:
SELECT friend_id FROM friends WHERE user_id = id; [fill in the id]
I'll give you something fancier:
SELECT * FROM members AS m
WHERE m.id
IN (SELECT f.friend_id FROM friends AS f
WHERE f.user_id = (SELECT pm.id FROM members AS pm
WHERE pm.username = 'amindzx'));
Granted using a join over a sub-query would be better.
Also, there's no need for an id in the friends column, because only one relationship between a user_id and a friend_id should exist; these can both be described as the id columns in unison.

Categories