I am trying to retrieve the ‘Roles’ sets of a given users.id on a query with INNER JOIN combined with a WHERE condition. But things go wrong.
My database has four tables:
t_users : id, username, userpass, status, ...
t_action: id, id_user, id_role, id_type_role, ...
t_role: id, libelle, status
t_type_role: id, libelle, status
My query:
SELECT U.id AS ID, R.libelle AS ROLE, T.libelle AS TYPE
FROM t_user U
JOIN t_action A ON A.id_user = U.id
JOIN t_type_role T ON T.id = A.id_type_role
JOIN t_role R ON R.id = A.id_role
WHERE A.id_user = '1' AND R.libelle = 'System'
But this query returns no data. (Tested on a phpmyadmin SQL board.)
2
Answers
as Akina has already mentioned in the comment section, there is no "libelle" value in the table "role" which equals ‘Système’ as you has mentioned it above. That is the reason why you do not get any output. Fix it to ‘System’ in the MySQL database and try it out again.
Use:
https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=20570938deb2bec281d5070dd28bf19d
Don’t put single quotes on integers, change
WHERE a.id_user ='1'
toWHERE a.id_user = 1
.libelle = 'System'
is in thetype_role
table not in therole
table