Many to many join - mysql

I have this
and I am trying to select all users that are friends with a user given by username.
I tried something like this
select friends.id_friend
from friends inner join users on users.id = friends.id_user
where users.Username = 'Dani'
but I want access to the fields from users table.

Youre going to need to join to the user table twice, like this (change SELECT to fit your needs):
select user.*, friend.*
from friends user_friend_link
inner join users user on user.id = user_friend_link.id_user
inner join users friend on friend.id = user_friend_link.id_friend
where user.Username = 'Dani'!
In this case, friends is a linking table between records in the users table. It associates one user record with another user record. To get user record info on both entities in the link table, you have to join it to both linking ids.

SELECT friends.ID_friend, u.*
FROM friends
INNER JOIN users
ON users.ID = friends.ID_User
LEFT JOIN users u
ON u.ID = friends.ID_friend
WHERE users.Username = 'Dani'

Related

Filtering an inner join results

I have four tables.
usuarios
roles
permisos
roles_permisos (pivot table)
I need to get all the users and roles with the permission equal to 2. I'm not good with sql and I don't know how to achieve this.
I guess I need an inner join between users and roles and from there filter the role with permission 2 from the pivot table
SELECT usuarios.nombre, roles.nombre
FROM usuarios
INNER JOIN roles ON roles.id = usuarios.rol_id
http://sqlfiddle.com/#!9/470356/16
PD: sorry, the examples are in spanish
Add another join with roles_permisos
SELECT usuarios.nombre, roles.nombre
FROM usuarios
INNER JOIN roles ON roles.id = usuarios.rol_id
INNER JOIN roles_permisos ON roles.id = roles_permisos.roles_id
WHERE roles_permisos.permisos_id = 2
You can start from the table for the permissions (permisos).
Where the id equals 2, or the name (permiso) equals 'Editar'.
Then join the other tables on the correct id.
Till you get to the table with the users (usuarios)
SELECT
usuarios.nombre AS usuario_nombre
, roles.nombre AS role_nombre
FROM permisos
JOIN roles_permisos
ON roles_permisos.permisos_id = permisos.id
JOIN roles
ON roles.id = roles_permisos.roles_id
JOIN usuarios
ON usuarios.rol_id = roles.id
WHERE permisos.permiso = 'Editar';

select all posts with different post id in mysql

SELECT *
FROM post p
JOIN user u ON p.user_id = u.id
JOIN friendships f ON f.friend_id = u.id
WHERE f.user_id = 1 OR u.id = 1
ORDER BY p.created_at DESC;
working on a projects where I'm trying to get all the post of the user as well as the user currently on.
So far i have this query working but is giving me duplicate posts of users.id = 1
is a user self join many to many where each user become friends and each user has their posts
The problem is probably because you select all columns from all tables involved.
Now for every new friendship with friend_id=1 you will receive a new record, with post duplicated. I guess you need:
select distinct p.*

sql query using results of prior query

How can I query a table and pull out same column info from foreign keys that reference the same table? "SELECT name FROM users INNER JOIN gifts ON to=id WHERE id=1;" will get me the first part but I am unsure on how to get the second part.
You can join the users table twice:
select
u1.name `from`,
u2.name `to`
from gifts g
join users u1 on g.`to` = u1.id
join users u2 on g.`from` = u2.id
where u1.id = 1;

How to select fields from three different tables in MySQL

This is driving me crazy, i've spent hours in this and i just don't know what to do:
i have 3 tables:
users
companies
company_permissions
in users i have coduser and username
in companies codcompany and company_name
and in company_permissions i have idcompany_permissions, codcompany, coduser
what i want to do is basically, with one query, get the name of the company to which the given username has permissions to access.
i had this query:
select users.coduser, users.username, companies.company_name
from users
inner join company_permissions on users.coduser = company_permissions.coduser
inner join companies on companies.codcompany = company_permissions.codcompany
where users.username = 'test'
But it doesn't return anything :C (it did at one point but now it doesn't do it)
there is no error with your query
check the database if the user 'test' exists
if it's exist because you using inner join it must be records in all three tables
check them again
I think for this case, you should use LEFT JOIN instead of INNER JOIN.
Anyway, WHERE users.user = 'test', I guest the field must be username.
Test your statement step by step
1.)
select users.* from users
--inner join company_permissions on users.coduser = company_permissions.coduser
--inner join companies on companies.codcompany = company_permissions.codcompany
where users.username = 'test'
is there a result... user-data is exist
2.)
select users.*
from users
inner join company_permissions on users.coduser = company_permissions.coduser
--inner join companies on companies.codcompany = company_permissions.codcompany
where users.username = 'test'
is there a result... the join between company_permissions and users is ok (data exist)
3.)
select users.*
from users
--inner join company_permissions on users.coduser = company_permissions.coduser
inner join companies on companies.codcompany = company_permissions.codcompany
where users.username = 'test'
is there a result the join between companies and company_permissions is ok..

mysql query: display all entries from guestbook_comments for a specific owner_id

I need some help with query from multiple tables.
My database:
I have following tables:
1. users (user_id, user_name, ..) //user_name is unique.
2. guestbook_comments(owner_id, comment_author_id, comment ..)
3. profile_photo(profile_photo_id, user_id, path)
My problem:
I want to build a following query:
I have url like guestbook.php?user=sudeep. So i get user_name by $_GET['user']; user_name is unique (in the users table).
I want to display all entries from guestbook_comments for a specific owner_id (but i only know user_name, so i will need to find user_id from users table).
For each of the comment_author_id in guestbook_comments table, I want to get the his user_name from table users and path from profile_photo table.
My approach:
First I join users and guestbook_comments table to get all guestbook comments for a specific user_id. Then In the while loop I join users and profile_photo table to get user_name and photo path respectively.
I highly doubt if my approach is any efficient. Can you tell me if there is a right way to do that?
$sql = "SELECT u.user_id, g.owner_id, g.comment_author_id
FROM guestbook g
INNER JOIN users u ON u.user_id = g.owner_id
WHERE u.user_name = $user_name";
while($row = mysql_fetch_array( $result )) {
$author_id = $row['comment_author_id'];
$sql = "SELECT u.user_name, p.path
FROM profile_photo p
INNER JOIN users u ON u.user_id = p.user_id
WHERE u.user_id = $author_id;
//display comment text from guestbook, user_name from users, and photo from profile_photo
}
Am I missing something, or could you combine both joins at once and get all the data with 1 call to the database, rather than 2 calls per user?
$sql = "SELECT u.user_id, g.owner_id, g.comment_author_id, p.path
FROM guestbook g
INNER JOIN users u ON u.user_id = g.owner_id
INNER JOIN profile_photo p on u.user_id = p.user_id
WHERE u.user_name = $user_name";
Try a single query:
SELECT u.user_id, g.owner_id, g.comment_author_id, g.comment_text, c.user_name, p.path
FROM users u
LEFT JOIN guestbook g
ON u.user_id = g.owner_id
LEFT JOIN users c
ON c.user_id = g.comment_author_id
LEFT JOIN profile_photo p
ON p.user_id = g.comment_author_id
WHERE u.user_name = $user_name
From user, find guestbook entries, find commenters, find commenters' photos