How do i display friends of a user in mysql? - php

I am creating a friend list module.
I have 2 tables: users table and friends table. I want the program to display the friends of a user without displaying the user as one of his/her own friend.
My table looks like
Users table
---------- ---------- ---------
user_id | username | password
-------------------------------
1 | elexis | *******
-------------------------------
2 | rooney | *******
-------------------------------
3 | wayne | *******
-------------------------------
4 | June | *******
-------------------------------
Friends table
---------- ----------
user_id | friend_id
---------------------
1 | 3
-----------
2 | 1
-----------
2 | 4
-----------
1 | 2
Expected output of user_id 1 (who is elexis) should be
User_id friend_id Username
1 3 Wayne
2 1 Rooney
My code is thus:
SELECT t1.user_id,t1.friend_id, t2.username
FROM friends AS t1
LEFT JOIN users AS t2
ON (t1.user_id = t2.user_id)
OR (t1.friend_id = t2.user_id)
WHERE t1.user_id = 1
The output I'm getting is:
User_id Friend_id Username
1 5. Elexis
1 1 Elexis
What am I getting wrong?

Join Users to a query that contains distinct pairs of user with id = 1 and each pair:
SELECT f.*, u.username
FROM Users u
INNER JOIN (
SELECT user_id, friend_id FROM Friends WHERE user_id = 1
UNION
SELECT friend_id, user_id FROM Friends WHERE friend_id = 1
) f ON f.friend_id = u.user_id;
Or, if you want to pass the user parameter only once:
SELECT f.*, u.username
FROM Users u
INNER JOIN (
SELECT user_id, friend_id FROM Friends
UNION
SELECT friend_id, user_id FROM Friends
) f ON f.friend_id = u.user_id
WHERE f.user_id = 1
See the demo.

You need to join on Friend_id and select user_id from Friends table.
SELECT f.user_id,f.friend_id,u.username
FROM friends f JOIN users u
ON f.user_id=f.friend_id
This gives you output:
user_id | friend_id | username
1 | 3 | Wayne
2 | 1 | Elexis

You can union two qieroes which give you the result
CREATE TABLE friend
(`user_id` int, `friend_id` int)
;
INSERT INTO friend
(`user_id`, `friend_id`)
VALUES
(1, 3),
(2, 1),
(2, 4),
(1, 2)
;
CREATE TABLE users
(`user_id` int, `username` varchar(6), `password` varchar(7))
;
INSERT INTO users
(`user_id`, `username`, `password`)
VALUES
(1, 'elexis', '*******'),
(2, 'rooney', '*******'),
(3, 'wayne', '*******'),
(4, 'June', '*******')
;
SELECT f.*, u. username FROM friend f JOIN users u ON f.friend_id = u.user_id WHERE f.user_id = 1 AND f.`friend_id` NOT IN (SELECT user_id FROM friend WHERE friend_id = 1)
UNION
SELECT f.*, u.username FROM friend f JOIN users u ON f.user_id = u.user_id WHERE friend_id = 1
user_id | friend_id | username
------: | --------: | :-------
1 | 3 | wayne
2 | 1 | rooney
db<>fiddle here

Related

find user2 friends + mutual count with user1

mysql to find user2 Friends and get mutual_count with user1 ( with every user2 friend)
Table users
user_id | username
------------------
1 | user1
2 | user2
3 | user3
4 | user4
5 | user5
table friends
user_one_id | user_two_id
------------------------
1 | 2
1 | 4
2 | 3
2 | 4
3 | 1
5 | 2
Expected output //( user2 friends : check result user_id with user1 to get mutual count)
user_id | mutual_count
------------------------
1 | 2 // user1 and user1 mutual `users(user2,user3,user4)`
3 | 1 // user3 and user1 mutual users(user2)
4 | 1 // user4 and user1 mutual users(user2)
5 | 0 // user5 and user1 -> No mutual users
mysql statement
SELECT users.user_id,
(SELECT count(a.friendID) FROM
( SELECT user_two_id friendID FROM friends WHERE user_one_id = users.user_id
UNION
SELECT user_one_id friendID FROM friends WHERE user_two_id = users.user_id
) AS a
JOIN
( SELECT user_two_id friendID FROM friends WHERE user_one_id = 1
UNION
SELECT user_one_id friendID FROM friends WHERE user_two_id = 1
) AS b
ON a.friendID = b.friendID
) as mutual_count
FROM friends LEFT JOIN users
ON friends.user_one_id = users.user_id or friends.user_two_id = users.user_id
WHERE (friends.user_one_id = 2 OR friends.user_two_id = 2) AND users.user_id != 2
Error I get
Unknown column 'users.user_id' in 'where clause'
problem subquery to get mutual_count canot know users.user_id
any help appreciated
Your hunch to use a union between the friends table was on the right track. The issue here is that a relationship between friends is only recorded in one direction, but we really need to detect each relationship regardless of direction. The UNION trick allows each relationship to be counted properly.
In the query below, the base table is this union. We self join it, on the conditions that:
The first table's user_one_id is 2
The first table's user_two_id matches to the second table's user_one_id
The second table's user_two_id is not 2, which would just be a cycle back to where we started
Then, we just aggregate by the first table's user_two_id and count the number of distinct friends to get the result.
SELECT
t1.user_two_id AS user_id,
COUNT(DISTINCT t2.user_two_id) AS mutual_count
FROM
(
SELECT user_one_id, user_two_id
FROM friends
UNION ALL
SELECT user_two_id, user_one_id
FROM friends
) t1
LEFT JOIN
(
SELECT user_one_id, user_two_id
FROM friends
UNION ALL
SELECT user_two_id, user_one_id
FROM friends
) t2
ON t1.user_one_id = 2 AND
t1.user_two_id = t2.user_one_id AND
t2.user_two_id <> 2
WHERE
t1.user_two_id <> 2
GROUP BY
t1.user_two_id
ORDER BY
t1.user_two_id;
Demo

MYSQL Select from tables based on multiple rows

I have a table called user_meta. In that table I have the following columns: ID, userID, meta_key, meta_value
I have another table called users, the only important column there is ID, which I want to compare to the user_meta table rows.
The users table looks like:
ID | email | etc...
1 | email#test.com |
5 | testa#a.com |
6 | .... |
7 | .... |
So say I have a table (user_meta) that looks like:
ID | userID | meta_key | meta_value
2 | 1 | companyID | 2
3 | 1 | user_type | staff
4 | 5 | companyID | 2
5 | 5 | user_type | staff
6 | 6 | companyID | 4
7 | 6 | user_type | customer
I want to retrieve a single row for each userID, but only if the company ID and user_type are correct.
I want to retrieve all users that have the same companyID that I would send in the query, so let's say $companyID=2, and then all users that have the user_type='staff'.
So user_meta.userID must equal users.ID, and user_meta.companyID must equal 2, and user_meta.user_type must equal 'staff'.
I want a list of all users that match these criteria.
A result would be userID 1 & 5 are returned. They both have companyID = 2, and both have user_type = staff
You need to join with user_meta once for each attribute you want to match.
SELECT u.*
FROM users AS u
JOIN user_meta AS m1 ON u.id = m1.userID
JOIN user_meta AS m2 ON u.id = m2.userID
WHERE m1.meta_key = 'companyID' AND m1.meta_value = :companyID
AND m2.meta_key = 'user_type' AND m2.meta_value = 'staff'
Not very sure about your question. I'm assuming this is what you may want:
select * from Users where ID in (
select userID from user_meta where (meta_key = 'companyID' and meta_value = 2) or (meta_key = 'user_type' and meta_value = 'staff')
);
SELECT `users`.`id`,
`Company`.`meta_value`,
`UserType`.`meta_value`
FROM `users`
JOIN `user_meta` `Company`
ON `Company`.`userid` = `users`.`id`
JOIN `user_meta` `UserType`
ON `UserType`.`userid` = `users`.`id`
WHERE `UserType`.`meta_value` = 'staff'
AND `Company`.`meta_value` = 2
https://gyazo.com/de8d9124418f65b993d708c80c309325

How do I make this select query?

I want to create a SELECT query in mysql.
There are two tables, users and image_info, and need to select following columns.
user table : user_id, username, dob
image_info : image, image_path
When selecting image. I need to get only primary image from image_info table. In image_info table there is a column like:
image_type ENUM('primary', 'gallery') DEFAULT NULL,
This is how I tried it..
$q = "SELECT u.user_id, u.username, u.dob, i.image, i.image_path
FROM users u
INNER JOIN image_info i ON i.user_id = u.user_id
WHERE u.sex = 'Male'
ORDER BY u.date_registered DESC LIMIT 6";
But it doesn't work properly to get my expected output.
UPDATE:
my table outputs..
mysql> select user_id, username, sex from users;
+---------+-------------+--------+
| user_id | username | sex |
+---------+-------------+--------+
| 1 | thara1234 | Male |
| 2 | root234 | Male |
| 3 | kamal123 | Female |
| 4 | Nilantha | Male |
| 5 | Ruwan324324 | Male |
+---------+-------------+--------+
5 rows in set (0.00 sec)
mysql> select user_id, image, image_type from image_info;
+---------+----------------------------+------------+
| user_id | image | image_type |
+---------+----------------------------+------------+
| 2 | 2_root234_1433564588.jpg | primary |
| 1 | 1_thara1234_1433555104.jpg | primary |
| 1 | 1_thara1234_1433556481.jpg | gallery |
| 4 | 4_Nilantha_1433573768.jpg | primary |
+---------+----------------------------+------------+
4 rows in set (0.03 sec)
Thank you.
I think, query would be :-
SELECT User.user_id, User.username, User.dob, Image.image, Image.image_path
FROM
users User LEFT JOIN image_info Image
ON User.user_id = Image.user_id AND Image.image_type = 'PRIMARY'
WHERE User.sex= 'Male'
ORDER BY User.date_registered DESC LIMIT 6
As you said you need the user either he has an image or not you should use left join in your query:
SELECT u.user_id, u.username, u.dob, i.image, i.image_path
FROM users u
LEFT JOIN image_info i ON i.user_id = u.user_id
WHERE u.sex = 'Male' and (i.image_type = 'primary' or i.image_type is null)
ORDER BY u.date_registered DESC LIMIT 6;
See here for more information.

SELECT multiple table

i need some help here,
table "friends"
+------+-------------+-----------+
id | friend_id | user_id |
+------+-------------+-----------+
1 | 1222 | 99999 |
+------+-------------+-----------+
2 | 48989 | 1492 |
+------+-------------+-----------+
table "users"
+------+-------------+---------------+
id | user_name | user_image |
+------+-------------+---------------+
99999 | Mark | img/abc.jpg |
+------+-------------+---------------+
1222 | Tom | img/xyz.jpg |
+------+-------------+---------------+
etc. | etc. | etc.. |
+------+-------------+---------------+
i want SELECT table friends and make WHERE statement :
etc : ... WHERE user_id=$_SESSION[user_id] ...
and will display data from table users
ok , let say :
my current id is 99999 so in table friends is match only 1222 , so this will display all data(image,etc..) from id 1222(Tom) from table users.
So my question here is how i need to write this code for generate users data ?
*i try to use UNION and LEFT JOIN..but no luck..still newbie..
$user_id = intval($_SESSION['user_id']);
$friends_of_user = mysql_query('
SELECT
f.*, u.*
FROM
friends f
LEFT JOIN
users u
ON
u.id = f.friend_id
WHERE
f.user_id = '.$user_id);
and to exclude all users which doesn't have profiles in users table, just change LEFT JOIN to JOIN
I'd use the following code:
$user_id = mysql_real_escape_string($_SESSION['user_id']);
$sql = "SELECT f.*
FROM users u
INNER JOIN friends f ON (u.user_id = f.friend_id)
WHERE u.user_id = '$user_id' ";

Mysql two table query

I'm using two tables. First (friendlist), which contains users who are on the list of friends and the other table (members) that contains the basic data of the users.
Friendlist looks like:
id | myid | date | user
-----------------------------------------
001 | 50624 | 2010-01-01 | 32009
002 | 41009 | 2010-05-05 | 50624
003 | 50624 | 2010-03-02 | 23007
The column "myid" contains members who added other users (those from column "user") to their frindlist. I want to get the list of all users that I have added to list and those who add me to their friendlist.
In this example, if my id is 50624, the list would look like:
| allfriends |
---------------
32009
41009
23007
Then I need to check all users from "allfriend" list with data from the table "members". I want to get only the users with status 1.
The members table looks like:
id | status | photo
--------------------------------
32009 | 0 | 1.jpg
41009 | 1 | 2.jpg
23007 | 1 | 3.jpg
How this mysql query should look like?
Thanks for any help.
SELECT id, status, photo FROM members WHERE id IN(
SELECT user FROM friendlist WHERE myid = 50624
UNION ALL
SELECT myid FROM friendlist WHERE user = 50624
) AND status = 1
SELECT user AS allfriends
FROM friendlist
INNER JOIN members
ON user = id
WHERE myid = 50624 AND status = 1
UNION
SELECT myid AS allfriends
FROM friendlist
INNER JOIN members
ON user = id
WHERE user = 50624 AND status = 1`
my friendlist:
select
members.*
from friendlist
inner join members
on members.id=friendlist.user
where friendlist.myid=50624 and members.status=1;
people who are friend with me
select
members.*
from friendlist
inner join members
on members.id=friendlist.myid
where friendlist.user=50624 and members.status=1;
To combine both results, use union
select
members.*
from friendlist
inner join members
on members.id=friendlist.user
where friendlist.myid=50624 and members.status=1
union
select
members.*
from friendlist
inner join members
on members.id=friendlist.myid
where friendlist.user=50624 and members.status=1;
Something like this should work:
SELECT ALLFRIENDS.id, MEMBERS.status FROM members MEMBERS
JOIN (SELECT id FROM (
SELECT myid AS id FROM friendlist WHERE user=50624
UNION
SELECT user AS id FROM friendlist WHERE myid=50624
) AS tmp
) AS ALLFRIENDS ON ALLFRIENDS.id = MEMBERS.id
WHERE MEMBERS.status = 1;

Categories