I have a database named 'friends' and it has columns 'user_id' and 'friend_id'.
user_id is the invitor and friend_id the recipient. Please note that when a friendship is created I'm NOT making 2 records in the database like 1,2 and 2,1.
How to list all the friends of mine, considering that my users.user_id can vary between friends.friend_id and friends.user_id in the 'friends' table. Also how to join the query to the 'users' table to get the names of all my friends.
One option would be a union of two queries:
select u.name
from friends f
inner join users u
on f.user_id = u.user_id
where f.friend_id = #YourID
union
select u.name
from friends f
inner join users u
on f.friend_id = u.user_id
where f.user_id = #YourID
You should make a Join query to get the best performance. Try something like this:
$friends_result = mysql_query("
SELECT
users.`name` AS name
FROM
friends
LEFT JOIN
users
ON
users.`id` = friends.`user_id`
OR
users.`id` = friends.`friend_id`
AND
users.`id` != '" . $user_id . "'
WHERE
friends.`user_id` = '" . $user_id . "'
OR
friends.`friend_id` = '" . $user_id . "'");
echo "<strong>My friends:</strong><br />";
while($friends_array = mysql_fetch_array($friends_result))
{
echo $friends_array['name'] . "<br />";
}
I think it would probably be easiest to ensure that the friends table always has the lower ID in the user_id column and the higher one in the other.
This way, you do not need to check twice, thus achieving better performance.
However, this approach requires you to modify your friend-adding code and assumes that user IDs never change (or you have to update your IDs in the table).
Otherwise, use UNION queries like Joe said or to join using an OR condition (e.g. JOIN ... ON u.user_id = friends.user_id OR u.user_id = friends.friend_id) or IN (JOIN ... ON u.user_id IN (friends.user_id, friends.friend_id)), which might be expensive operations (use EXPLAIN and benchmarks to find out).
Related
I have two tables: publick_feed and users
I want to SELECT all from public_feed and also SELECT a three columns from users whose id is the same of user_id in public_feed
and assign the rows returned from public_feed to the column in users table ( correspondent)
I try this:
<?php
$sql = "
SELECT * FROM public_feed
WHERE user_id IN
(SELECT id FROM users) AND
(SELECT Firstname,Lastname,Avatar FROM users WHERE id IN(SELECT user_id FROM public_feed))
";
$query = mysqli_query($dbc_conn,$sql);
if(mysqli_num_rows($query) > 0){
while($row = mysqli_fetch_assoc($query)){
//echo rows with correspondent details from the users table
echo $row['user_id'];
}
}
<?
Please any help will be much appreciated.
Thank you.
Or version with left join in case if there is no user in public_feed, and you still want to fetch user data
SELECT
u.*, f.*
FROM
public_feed f LEFT JOIN
users u ON f.user_id = u.id;
Because author asked for explanation, here it is:
First we are going to use table name alias to make query shorter
public_feed f
and
users u
we are saying that want to refer to tables with an alias. Of course * means that we want to select all columns
SELECT users.*, public_feed.*
is equal to
SELECT u.*, f.*
Of course you can use any other letters as an alias
Next we are saying that public_feed.user_id must be equal to users.id. But when public feed entry does not exists just display columns with null values. This is why we are using LEFT JOIN instead of INNER JOIN. In general JOINS are used to fetch related data from more than one related tables.
ON keyword is saying values from which columns in the tables must be equal to satisfy the request
I think doing a join would be cleaner than using a complicated subquery:
SELECT u.Firstname,
u.Lastname,
u.Avatar,
COALESCE(pf.User_id, 'NA'),
COALESCE(pf.Post, 'NA'),
COALESCE(pf.Date, 'NA')
FROM users u
LEFT JOIN public_feed pf
ON u.Id = pf.User_id
I chose a LEFT JOIN of users against public_feed on the assumption that every feed will have an entry in the users table, but not necessarily vice-versa. For those users who have no feed entries, NA would appear in those columns and that user would appear in only a single record.
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.
I have the following tables and would like to query one more element from them.
categories table ->idcat(int), cat(varchar);
topics table ->idtopic(int), topic(varchar), idcat(int-fk), iduser(int-fk);
replies table ->idreply(int), reply(varchar) iduser(int-fk), idtopic(int-fk)
users table ->iduser(int), username(varchar).
My current query is;
$query = "SELECT t.topic, t.idtopic, u.username
FROM topics t
LEFT JOIN categories c ON t.idcat = c.idcat
LEFT JOIN users u ON t.iduser = u.iduser
WHERE c.idcat = '" . $idcat . "'";
Which presents 'Topic' and 'Username'. I'd like to show 'idReply' as well but don't know the proper JOIN syntax.
SELECT
t.topic,
t.idtopic,
u.username
FROM
topics t
LEFT JOIN
categories c ON t.idcat = c.idcat
LEFT JOIN
users u ON t.iduser = u.iduser
LEFT JOIN // new
replies r ON r.iduser = u.iduser AND r.idtopic = t.idtopic // new
WHERE c.idcat = '" . $idcat . "'";
This will generate a row for every reply to every topic in the specified category. Which could be a lot of records.
You may also want to experiment with the exact type of join on the replies table to get the result you want. LEFT JOIN is probably correct as you'll still get a result if there's no reply to a given topic. This may depend on your flavour of SQL.
OUTER JOIN & LEFT OUTER JOIN are possibilities.
Using INNER JOIN will ensure only topics with replies are returned.
You must have reply_id (foreign key) in your topics table. After that you can use this query.
$query = "SELECT t.topic, t.idtopic, u.username, r.id as reply_id
FROM topics t
LEFT JOIN categories c ON t.idcat = c.idcat
LEFT JOIN users u ON t.iduser = u.iduser
LEFT JOIN replies r ON t.reply_id = r.id
WHERE c.idcat = '" . $idcat . "'";
Yes so im building an query from the advanced search form.
I have this:
$query = "SELECT * FROM users WHERE 1 ";
$query .= "AND sex = '$sex' ";
for now, next im going to have AND birthday.. but then i dont know how to do it, because users birthday is stored in users_profile
So please correct me, how can i:
$query .= "AND birthday in users_profile = '1'";
Is this possible even, or should i reconstruct it and put birthday in users instead..
update:
in users, the id column there, is binded with users_profileĀ“s uID.
So in users_profileĀ“s uID column, there is the users id.
I assume your users_profile table is linked to the users table?
SELECT u.*, up.birthday
FROM users u
INNER JOIN users_profile up
ON u.user_id = up.user_id
WHERE sex = '$sex'
Here an Inner Join is used. The reason we can use u instead of users and up instead of users_profile is because we have set up the aliases "users u" and "users_profile up"
You need to look at the syntax for JOIN.
You need a way to join individual related rows in the two tables, something like:
SELECT u.* FROM users u, users_profile p
WHERE u.sex = 'M'
AND p.birthday = '1'
AND u.userid = p.userid;
I don't understand why you have separate tables for user and for users_profile, but you need to JOIN the two tables:
SELECT U.*
FROM users U
LEFT JOIN users_profile P
ON P.uID = U.uID
AND P.birthday = '1'
WHERE U.sex = '$sex'
Very possible, given you have the foreign key to the users_profile table.
Let's say the primary key in the users table is named 'id', and the users_profile table contain a field called 'uid' which point to the users table, you'd normally create the query like this:
SELECT * FROM users u, users_profile p WHERE u.id = p.uid
AND u.sex = '$sex' AND u.birthday = 1
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.