[Solved] joining multiple tables and returning a zero if not row in one

EverSQL Database Performance Knowledge Base

joining multiple tables and returning a zero if not row in one

Database type:

I'm trying to setup a new permissions database in MySQL and I'm breaking my brain over something that I'm sure is very simple. I'm certain something to this tune has been answered here before but after hours of searching I have found nothing that works.

I have 4 tables that are relevant

Permission (contains every possible permission)

|permission_name | description |
--------------------------------
|users.list      | etc. etc.   |
|users.update    | etc. etc.   |
|users.delete    | etc. etc.   |

User

| id      | fname | group_id |
------------------------------
| 1       | John  | 1        |
| 2       | Nancy | 1        |
| 3       | Paul  | 2        |

Group

| group_id | group_name |
-------------------------
| 1        | Webmasters |
| 2        | Corporate  |
| 3        | HR         |

Group_permission (contains permissions relevant to each group)

| group_id | permission_name | permission_type (1=Y|0=not set|-1=N)
----------------------------------------------
| 1        | users.list      | 1              |
| 1        | users.update    | 1              |
| 2        | users.list      | 1              |

OK so lots of relations going on, but I'm trying to get ALL the group permissions for a specific user EVEN if the group permission doesn't exist yet.

I imagined this being some sort of left join using a permission table as a base, but whenever I include the WHERE user.id = 2 it limits my result set down and won't include nulls on the right side.

SELECT a.permission_name, IFNULL(b.permission_type, 0)
FROM permission a
LEFT JOIN group_permission b on b.permission_name = a.permission_name
LEFT JOIN user c on c.group_id = b.group_id
WHERE c.id = 2

the result I want to see for Nancy is

|permission_name | permission_type |
------------------------------------
|users.list      | 1               |
|users.update    | 0               |
|users.delete    | 0               |

I won't know what group the user is in on the PHP side, so I have to query by using the users ID only.

All I'm getting is

|permission_name | permission_type |
------------------------------------
|users.list      | 1               |

Any help appreciated. TIA

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  2. Prefer Inner Join Over Left Join (modified query below): We identified that one or more left joined entities (e.g. `user`) are used in the 'where' clause, in a way that allows to replace it with an optimized inner join. Inner joins can be fully optimized by the database, while Left joins apply limitations on the database's optimizer.
Optimal indexes for this query:
ALTER TABLE `group_permission` ADD INDEX `group_permission_idx_group_id` (`group_id`);
ALTER TABLE `permission` ADD INDEX `permission_idx_permission_name` (`permission_name`);
ALTER TABLE `user` ADD INDEX `user_idx_id` (`id`);
The optimized query:
SELECT
        a.permission_name,
        IFNULL(b.permission_type,
        0) 
    FROM
        permission a 
    LEFT JOIN
        group_permission b 
            ON b.permission_name = a.permission_name 
    INNER JOIN
        user c 
            ON c.group_id = b.group_id 
    WHERE
        c.id = 2

Related Articles



* original question posted on StackOverflow here.