T
T
Talyan2018-03-28 07:35:45
MySQL
Talyan, 2018-03-28 07:35:45

Am I doing the INNER JOIN correctly?

Good morning.
There are tables in the database (an example of an entry in brackets):

premissions :				        id	name //(1,"page_edit")
group_premissions: 			        id	group_id	premission_id //(1,1)
user_groups:				        id	group_name //(1,"Редакторы")
users:						id	group_id //(1,1)

premissions - privileges
user_groups - list of groups
users - users
group_premissions - link between the group and the privileges granted to it
We need a query that pulls the user's privileges into an array of privileges, which is then stupidly checked before loading the page
if(in_array("page_edit"),$user->get_access ()) { load the page } else { die("You shouldn't be here!");}
I made this request:
SELECT
premissions.name,
group_premissions.group_id,
group_premissions.premission_id,
users.id as userid,
user_groups.group_name
from group_premissions
inner join premissions on group_premissions.premission_id=premissions.id 
inner join user_groups on user_groups.id=group_premissions.group_id 
inner JOIN users on users.group_id=user_groups.id 
WHERE users.id='1'

Everything works great. But suddenly the question arose - am I using INNER JOIN correctly? maybe you should use LEFT or RIGHT JOIN? I have never used anything other than INNER JOIN in my life. And in the first steps, he generally wrote this:
SELECT A.value1, A.value2, B.value1, B.value2 FROM A,B where A.value1=1 AND A.value2=B.value1

I read about overlapping LEFT and RIGHT JOIN queries and how they "conjunct and disjunct" the results, but I still don't fully understand - where such joins might be needed, why, and am I using INNER correctly?
Or how? Works - do not touch? =)) Open my eyes so I can take off my tinfoil hat.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abroabr, 2018-03-28
@flapflapjack

It's actually easier than it looks.
An inner join is the most common table join. It is not even necessary to indicate that this is an inner join, if there is a simpler syntax, it will still be an inner join.
An inner join means that as a result you will get what was in both tables at once. If at least one of the tables did not have a record matching the join condition, you will not get the corresponding row.
An outer join means that in addition to what an inner join would give you, you will additionally receive data from the first (left join) or second (right join) table. Even if there were no records in the other table that matched the join condition.
Why would you need an outer join? For example, you have users who are not members of any group. By building an outer left join on the users table and on the groups table, you will get a complete list of users. Those of them that are not included in any group will also be present in the list, but NULL will be indicated in the columns describing the groups.
If you build exactly the same query on an internal (normal) connection, that is, connect users and groups, you will get only those who are part of at least some group.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question