Filtering an inner join results - mysql

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';

Related

mysql JOIN between three tables

I have 4 tables in my database:
users (id,name)
roles (id,name)
positions (id,name)
position_user (user_id,position_id)
Relationship between users to roles is one to one
Relationship between users positions is many to many
i want to take all users with their role name and list with their positions but i don't know how to structure my query. I think that one of my query must be something like this:
SELECT pu.user_id AS user_id,
group_concat(p.name separator ',') AS list_pos
FROM position_user pu
INNER JOIN positions p
ON p.id = pu.position_id
GROUP BY pu.user_id
And other one must be like this :
SELECT users.id, users.first_name, roles.name
FROM users
JOIN roles
ON users.role_id = roles.id
Can I combine these two in one query and how ?
Try something like this and check the MySQL documentation.
SELECT pu.user_id AS user_id, u.first_name, r.name as rol_name, group_concat(p.name separator ',') AS list_pos
FROM position_user pu
INNER JOIN positions p ON p.id = pu.position_id
INNER JOIN users u ON u.id = pu.uder_id
INNER JOIN roles R ON u.role_id = r.id
GROUP BY pu.user_id, u.first_name, r.name

Many to many join

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'

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..

How to do MySQL query to fetch other table field value, in case of existance?

Suppose we have this model:
As you see industry_id can be null. Can I fetch industry.name (if any), user.description, profile.name and project.title (all project titles) he/she has with a single MySQL query while having user.id?
Yes, JOIN the two tables:
SELECT
i.name,
u.id
FROM Industry AS i
LEFT JOIN `User` AS u ON u.industry_id = i.industry_id;
Update:
For multiple tables:
SELECT
i.Name AS InustryName,
p.Name AS UserName,
u.Description,
j.title AS ProjectTitle
FROM Industry AS i
INNER JOIN User AS u ON i.id = u.id
INNER JOIN Profile AS p ON p.user_id = u.id
INNER JOIN Project AS j ON u.id = j.user_id;
Note that: I used INNER JOIN between the tables, this will give you only the matched rows from the joined tables, you might need to use LEFT JOIN instead of innner join to include the untmatched rows, i.e., to get those industries that has no entries in the other tables. See this blog post:
A Visual Explanation of SQL Joins

SQl queries left join not giving accurate results

I have been trying to join two tables (USERS AND USERS_ROLES) based on their role id I put the left join on following query
users.id = users_roles.fk_user_id
but the output is not correct of users_roles.fk_role_id coulmun and shows NULL where it should display the id of users.id = users_roles.fk_user_id that is 4 (at most places) because on users.id = users_roles.fk_user_id the value of users_roles.fk_role_id = 4
Kindly let me know how can i fix that so my query should result the exact vlaues of ids where they match,
Thanks
SELECT users.id, users.v_first_name, users.v_last_name, user_facility.fk_facility_id,users.fk_tenant_id, marital_status.v_marital_status,
users.v_blood_type, NOW(),users_roles.fk_role_id
FROM users
LEFT JOIN (user_facility, marital_status, users_roles) ON
users.id = user_facility.fk_user_id AND users.fk_marital_status_id=marital_status.id AND users.id = users_roles.fk_user_id
Usage of AND operator when used with Left or Right join gives different result. You should be clear what you are trying to accomplish..See this
well it is what you get by first implicitly inner-joining 3 tables and then explicitly left-joining the result to a 4th table only if 3 conditions relevant to all of the 3 inner-joinded tables are matched (i.e. when 3rd condition is false, nothing is joined from either of the 2 remaining tables)
i strongly suggest not to combine implicit and explicit joins, i personally use explicit joins all the time:
if you need an outer join:
SELECT ...
FROM users
LEFT JOIN user_facility ON users.id = user_facility.fk_user_id
LEFT JOIN marital_status ON users.fk_marital_status_id=marital_status.id
LEFT JOIN users_roles ON users.id = users_roles.fk_user_id
if you need an inner join:
SELECT ...
FROM users
JOIN user_facility ON users.id = user_facility.fk_user_id
JOIN marital_status ON users.fk_marital_status_id=marital_status.id
JOIN users_roles ON users.id = users_roles.fk_user_id
or if you prefere implicit inner joins for some obscure reason:
SELECT ...
FROM users,
user_facility,
marital_status,
users_roles
WHERE users.id = user_facility.fk_user_id
AND users.fk_marital_status_id=marital_status.id
AND users.id = users_roles.fk_user_id
(implicit outer joins are getting deprecated in all RDBMS as far as i know)
When it shows NULL it means there isn't a correspondency (relation) between all tables in the JOIN clause.
If you want to show only the ones that have relations in all tables, use INNER JOIN instead.
SELECT u.id,
u.v_first_name,
u.v_last_name,
uf.fk_facility_id,
u.fk_tenant_id,
ms.v_marital_status,
u.v_blood_type,
NOW(),
ur.fk_role_id
FROM users u
INNER JOIN user_facility uf ON u.id = uf.fk_user_id
INNER JOIN marital_status ms ON u.fk_marital_status_id=ms.id
INNER JOIN users_roles ur ON u.id = ur.fk_user_id