Getting friend list from MySQL using PHP - php

I have to tables in MySQL database, USERS and FRIENDS. They have this structure:
USERS TABLE:
FRIENDS TABLE (two users are friends ONLY if exists two rows like in the image):
I want to create a MySQL query in PHP to SELECT the nombre (name) of the users that are my friends. I read that I have to use the operator LEFT JOIN but I did not succeed.
Thank you!

Try using INNER JOIN instead. Try below query -
SELECT users.nombre FROM users INNER JOIN friends ON
(users.id = friends.usuari_o OR users.id = friends.usuari_t)
WHERE (friends.usuari_o = $id OR friends.usuari_t = $id);

I answer to myself.. IT WORKS! Can I do it efficiently? 3 selects in one query..
SELECT nombre FROM usuarios WHERE id IN
(SELECT usuari_one FROM friends WHERE usuari_two='$id')
AND (SELECT usuari_two FROM friends WHERE usuari_one='$id')

Related

How to join 2 values from different tables in mysql

I'm a beginner in MySQL, so I need help.
I want to get values of name column from table role to show it together with information from users instead id_role.
How can I realize it? What should the query I write?
Can you attach the link with solve?
The picture for explanation.
Read more here about SQL JOINS
SELECT users.*, role.*
FROM users
INNER JOIN role ON users.id_role = role.id;
Try LEFT JOIN
SELECT `users`.*,`role`.name as role FROM `users` LEFT JOIN `role` ON `users`.id_role= `role`.id

Getting user id from on table and counting them in another table

I have to make a scores page for a game and one thing it has to display is the number of times the current user has played the game. Basically what i need to select ID and username from one table, link them together and count the corresponding user IDs from another table.
These are the tables I was given: Users --> ID, USERNAME and Times_Played --> ID, USER_ID
Thanks in advance
You want to query something like this:
SELECT Users.USERNAME, Times_Played.User_ID
FROM Users
INNER JOIN Times_Played
ON Users.ID=Times_Played.ID;
EDIT
After re-reading the question and seeing the need to count the times played you could do this via PHP or via your SQL Query. Via PHP:
$result = mysqli_query(
"SELECT Users.USERNAME, Times_Played.User_ID
FROM Users
INNER JOIN Times_Played
ON Users.ID=Times_Played.ID"
);
$timesPlayed = mysqli_num_rows($result);
If it has to be done via the query, Anish has the correct solution.
I hope that helps.
Try this
select count(*) as Times_Played,Users.USERNAME,Users.ID
from Users
inner join Times_Played
on Users.ID = Times_Played.USER_ID

Displaying name of two users from a different table

Keeping this clean and to the point:
I have two MySQL tables, users (id, nick) and bans (id, banned, bannedBy, length).
I want to display in a table a list of bans, but instead of displaying the banned ID and the bannedBy ID, I want to display their nick.
I can use an JOIN to get the nick of one of them, in something like this:
SELECT bans.id,bans.banned,bans.bannedBy,bans.length,users.nick
FROM bans
JOIN users ON users.id=bans.banned
But then I can't get the bannedBy's nick, and vice verca.
I hope I was clear, thanks in advance for any help.
You have to join users table twice, on different keys.
SELECT
bans.id,
bans.banned,
bans.bannedBy,
bans.length,
u.nick as 'banedNick',
u2.nick as 'bannedByNick'
FROM
bans
JOIN
users u ON users.id = bans.banned
JOIN
users u2 ON users.id = bans.bannedBy
You can use two joins:
SELECT bans.id,bans.banned,b.nick as bannedBy,bans.length,u.nick
FROM bans
JOIN users u ON u.users.id=bans.banned
JOIN users b ON b.users.id=bans.bannedBy

Is it possible Mysql query by inner join

i would get details from two MySQL tables
tables structure as shown
table:App
|AppID|AppName|AppType|
table:AppRelease
|AppReleaseID|AppID|ReleaseDate|ReleaseVersion|
and written query as shown below
$query="
SELECT
A.*,
B.ReleaseDate,
B.ReleaseVersion
FROM App AS A
INNER JOIN AppRelease AS B
WHERE A.AppID = B.AppID
";
i get the values when appid is in both tables
but i also want to get values from App table though i dont have data in AppRelease release table
is it possible to write query please help me
Your requirement shouldn't be inner join.
Use left join:
$query= "SELECT A.*,B.ReleaseDate,B.ReleaseVersion
from App as A LEFT JOIN AppRelease as B
ON A.AppID=B.AppID";

php/mysql join syntax

I have the userids of people who have joined a group in one table but not their names thatlie in another table. So I think I need to do a join. I'm starting with groupid that describes the grouop.
Table 1, groupmem has groupid and userid for the members.
Table 2, users has userid and username
The users table has every user. The groupmem only has some who have joined groups.
SQL statement should be something like following but can't get it to work.
select users.name
FROM users,groupmem
LEFT JOIN users
on groupmem.userid=users.userid
WHERE groupmem.groupid = 22
22 being some value..
Thinking maybe where clause is in wrong place or I am using wrong type of join but or not using on correctly but, in any case, can't get it to work. Thx for any suggestions:
Try:
SELECT u.username
FROM `users` u
LEFT JOIN `groupmem` g
ON u.userid = g.userid
WHERE g.groupid = 22
This should do the trick:
SELECT users.name
FROM groupmem
LEFT JOIN users ON groupmem.userid = users.userid
WHERE groupmem.groupid = 22
Your query seems to be quite right - assuming I guessed your table-structure right.

Categories