(SQL) Get friends where user and friends are in 2 different tables - php

I've got 2 tables: users & friends. I want to show each profile picture of the friends of the logged user, but loggedUser-friends match is in a different table (which is friends) than the one that contains the URI to the profile pictures (which is users).
My query now looks like this:
SELECT profile_picture FROM users WHERE
And now I have to complete the WHERE clause:
friend's table schema is the following:
id | user_id | friend_id
friends table's sample data:
1 | pino20 | sebo14
while users' table schema is
id | user_id | profile_picture
users table's sample data:
1 | pino20 | pino20/profile.jpg

With below query you get your frends userid and their profile picture.
select user_id , profile_picture from users WHERE user_id in( select friend_id from friends where user_id=$your_userid)

Above result can be achieved using join
SELECT NT.profile_picture FROM users AS NT JOIN friends AS FR ON FR.id = NT.id

You can achieve this by using joins. It should look like this:
EDIT:
SELECT user.profile_picture,user_friend.profile_picture
FROM user
JOIN friend ON user.user_id = friend.user_id
left join user user_friend on user_friend.friend_id=friend.user_id -- left join since we don't want to drop records if there is no match

Related

Like structure table with Laravel and showing the most popular content at top

I am using Laravel to creating a website, my users can post questions and other users can write their comments under the post, each comment have Up vote and Down vote, and users can voting for comments.
I need most liked (Up vote) shows topper than others..
This is my database structure and I join them together:
comment table:
comment_id | question_id | user_id | timestamp
up and down votes table (like):
like_id | comment_id | like_type | user_id | timestamp
note:like_type is an enum on mysql and its values are upvote and downvote.
1-What is the mysql query and Laravel codes for that?
2-Is my database Structure right?
1.Calculate SUM belongs to Each Comment
2.Make it order by Desc
select * from (
select ct.comment_id,ct.question_id,
ct.user_id,SUM(case when vt.like_type='upvote' then 1 else -1 end )
cnt from commenttable ct
from votestable
vt left join
on
vt.comment_id=ct.comment_id
)D order by D.cnt desc

PHP - MYSQL - Messages/Users table join issue

I'm using MYSQL to display some data from specific tables... However I have small problem..
I'm trying to build some conversation system (for my learnin purpose)...
I query "database name messages" where I have 2 columns, user_one and user_two. The currently logged in user id can be eather user_one or user two (This fields are id's of users)...
So I use SELECT ... FROM table_name WHERE user_one = $id OR user_two = $id); and that display me messages properly where user_one or user_two is currently logged in user. It works fine...
However now I need to know how I join table users and pick only username of "other person", not logged one and display it on each message like "Message from..."!
So lets say I have following:
Users table:
id username
--------------------------
1 username1
2 username2
Messages table:
id title user_one user_two
--------------------------------------------
1 Title 1 2
2 Title2 2 1
So if username1 is logged in, when i output my messages i would like it to output:
Title From
-----------------------------------------
Title username2
Title2 username2
If username2 is logged in then it would say From: username1
So I want to display From username always of second person, not logged in one... I joined table regulary but it display me both users usernames...
Am I doing table structure wrong or how can I do that?
Thanks.
try this
SELECT Messages.title As title ,Users.username as `FROM` FROM Users
INNER JOIN Messages
ON Messages.user_two = Users.id
OR Messages.user_one = Users.id
WHERE Users.username != 'username1'
DEMO

Attempting to list data using ID from a particular table

I have one table which stores user info, including the username.
Another table contains a list of user id's and the user id's of those that they have favorited.
I am trying to figure out the query for listing the usernames of those that user id 1 has favorited.
In my query, assuming that I am uid 1, I need the usernames of uid 3 and 5, but instead
in sqlfiddle I am attempting to join them but I keep getting my username, cjaredrun, instead of the matched usernames for each favorite.
You can see what I have been trying here: http://www.sqlfiddle.com/#!2/c5836/1
Any guidance appreciated
You just need a simple join:
SELECT u.username FROM fav_user f
JOIN users u ON u.uid = f.matchuid
WHERE f.uid = 1
ORDER BY datetime
Fiddle here.
Output:
| USERNAME |
|----------|
| jolet |
| jane |

Using PHP to select and pair two users

I'm trying to make an application that pairs two available users by selecting them randomly from a database and then making them unavailable (so they can't be selected again).
I was wondering how I could select them and then pair them?
I've got the following database structure.
USERS
| id | autoincrement, primary key
| user_id | user's ID
| connected | available, connecting, chatting
ROOMS
| id |
| room_name |
| room_id |
| user_id | first user connected
| user_id2 | second user connected
I'm not making rooms, I'm simply trying to pair users and send them to a room.
Do you mean join the records?
You could do something like:
select * from rooms as r
left outer join users as u on r.user_id=u.id
left outer join users as u2 on r.user_id2=u2.id
where r.id=123
123 = a room id.
EDIT:
Here is a way to update a Rooms record with two random Users
UPDATE rooms
SET
user_id=(SELECT id FROM users ORDER BY RAND() LIMIT 0,1),
user_id2=(SELECT id FROM users ORDER BY RAND() LIMIT 0,1),
where id=123
I have tested this and it seems to work. This will keep you from having duplicate users:
UPDATE rooms
SET
user_id=(SELECT u1.id FROM users as u1 ORDER BY RAND() LIMIT 0,1),
user_id2=(SELECT u2.id FROM users as u2 where user_id <> u2.id ORDER BY RAND() LIMIT 0,1)
where id=123
The SELECT could be something like this:
SELECT user_id
FROM `USERS`
WHERE connected = 'available'
ORDER BY RAND()
LIMIT 2;
You would need to parse the resultset and assign the to user_ids to variables such as
$user_id1
$user_id2
The first UPDATE would be something like this:
UPDATE `USERS`
SET connected = 'connecting'
WHERE user_id IN ($user_id1,$user_id2);
The INSERT would look something like this:
INSERT INTO `ROOMS` (room_name,room_id,user_id,user_id2)
VALUES ($room_name,$room_id,user_id,user_id2);
Finally the last UPDATE
UPDATE `USERS`
SET connected = 'connected'
WHERE user_id IN ($user_id1,$user_id2);
Although you are probably going to want to lock rows I have not grasped that part of database management yet. For you purposes you might want to look into it.
According to me you'll need three tables instead of two to implement your logic.
Consider my db schema
USERS
| id | autoincrement, primary key
| user_id | user's ID
| connected | available, connecting, chatting
ROOMS
| id |
| room_name |
USERCHAT
| id |
| user_id | user's ID
| room_id | room id
To get pair you can execute following query.
SELECT user_id
FROM USERS
WHERE id NOT IN (SELECT user_id FROM USERCHAT)
ORDER BY RAND()
LIMIT 2;
After selecting users you can simply insert them in ROOMS and USERCHAT tables. Using this schema you can also allow multiple users instead of two for chat.

MySQL Table Joins

I have a table which links the userID and the friendID (which is another user) to say they are friends.
If User 1 adds User 24 as their friend, the first row in the below table will happen
If User 10 adds User 1 as their friend, the second row in the below table will happen
From there, even if User 1 is on either side, they are friends with the adjacent ID.
FRIENDS TABLE:
userID | friendID
-----------------
1 | 24
10 | 1
What I need to do now is: If User 1 is logged in, I need to display the people they are friends with. This query I have wrote:
SELECT DISTINCT (Users.username), friends.userID, friends.friendID FROM Users, friends WHERE Users.userID IN(SELECT userID FROM friends WHERE Users.userID = friends.userID) OR Users.userID IN(SELECT DISTINCT(userID) FROM friends WHERE Users.userID = friends.friendID)
Will bring back:
username | userID | friendID
-----------------------------
name1 | 10 | 1
name1 | 1 | 24
name2 | 10 | 1
name2 | 1 | 24
name1 = ID 1
name2 = ID 10
Is there a way where I can retrieve the logged in Users'friends (e.g. if ID #1 is logged in) whether they appear in the userID or friendID columns. and then, in this case, retrieve ID 24 and 10 because they are linked with ID #1, and then when it comes to displaying, eliminate ID # 1 from the list because it will bring up that they are friends with themselves.
Hope this makes sense and thank you in advance!
SELECT DISTINCT s.FriendId
FROM (
SELECT f.FriendId
FROM Friends f
WHERE f.UserId = #id
UNION ALL
SELECT f.UserId
FROM Friends f
WHERE f.FriendId = #id
) s
WHERE s.FriendId != #id
You may not need the last WHERE given that a user probably is not adding himself or herself as a friend.
While the way you are storing data may seem efficient, the following may be a better approach:
When a MUTUAL friendship is formed, enter the data twice: once as userID(1) friendID(24) and once as userID(24) friendID(1). The reason this approach may be good is that it makes your table reusable.
Presently your table is like facebook: if I am your friend then perforce you are also my friend: I see your activities; you see my activities. The design I explain allow you to use the table as both facebook and twitter: just because you are following my activities does not mean I want to follow yours.
Here's an easy way to do it with UNION. This also takes care of duplicates:
(SELECT userID
FROM friends
WHERE friendID = 1)
UNION
(SELECT friendID
FROM friends
WHERE userID = 1)
If you want to return usernames too, you can do your JOINs inside the subqueries:
(SELECT f.userID, u.username
FROM friends f
JOIN users u
ON u.userID = f.userID
WHERE f.friendID = 1)
UNION
(SELECT f.friendID, u.username
FROM friends f
JOIN users u
ON u.userID = f.friendID
WHERE f.userID = 1)

Categories