简体   繁体   中英

How to do an mySQL query that return unique values from different tables

Hi I have a big database consists of 100+ tables. I have 3 tables named auth_group(id and group_name), auth_permission(id and permission_name) and auth_group_permission(Associative table b/w these two tables) consists of fields id, group_id and permission_id. There are many groups inside the auth_group table and each auth_group may have many permissions too. Some groups may have same permissions as other groups. I needed to create an excel sheet which contains Group and permission, if same permissions are coming for different groups include in both the group rows . I needed to create a query that returns A list of groups and their corresponding permissions grouped together . Like example:

Managers : can create record
            can delete record
            etc.
 Engineer : create data
             delete data

I have tried the following query, but didn't worked out as subjected.

select distinct G.name, P.name from auth_group G, auth_permission P, auth_group_permission GP where G.id = GP.group_id AND P.id = GP.permission_id LIKE G.name LIKE 'BIG_RA_%'

Any idea guys? Thanks in advance.

You can try something like this.

SELECT G.name,
   GROUP_CONCAT(P.name)
 FROM  auth_group_permission GP
 INNER JOIN auth_group G ON G.id = GP.group_id
 INNER JOIN auth_permission P ON P.id = GP.permission_id
 WHERE G.name LIKE 'BIG_RA_%'
 GROUP BY G.name

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM