SQL Query complex tree join - mysql

I have a catalog that contains directories that can contain directories. The access of a directory can be granted to everyone or restricted to 0 to n user and/or to 0 to n usergroup. Access right is inherited in the hierarchy of group and from a group to its user. Therefore, a user can access a directory if he is allowed OR (he isn't deny AND (his group is allowed OR (his group isn't deny AND (his supergroup is allowed OR etc....
Their can be 1 to n level of groups, even if most of the time, their are typically no more than 3 levels.
I made a sqlfiddle from the schema : http://sqlfiddle.com/#!2/81b28
What I need is to get the list directories inside a given directory that can be accessible by a given user.
What I have so far is the following: this get the content of directory id '3' accessible to the user id '10', who is in usergroup '3' which is in supergroup '1' :
select l.id, l.accessright, ll.parentlibrarydirectoryid, ulc.*, glc1.*, glc2.*
from librarydirectory l
inner join a_librarydirectory_librarydirectory ll on ll.childlibrarydirectoryid = l.id
left join a_user_librarydirectory_canaccess ulc on ulc.librarydirectoryid = l.id
left join ws_user u on u.id = ulc.userid
left join a_group_librarydirectory_canaccess glc1 on glc1.librarydirectoryid = l.id
left join a_group_librarydirectory_canaccess glc2 on glc2.librarydirectoryid = l.id
where
ll.parentlibrarydirectoryid = 3
and (u.id = 10 or u.id is null)
and (glc1.groupid = 3 or glc1.groupid is null)
and (glc2.groupid = 1 or glc2.groupid is null)
and (l.accessright = "n"
or ((ulc.canaccess = "y")
or (ulc.canaccess is null and (glc1.allowedToAccess = 1
or (glc1.allowedToAccess is null and (glc2.allowedToAccess = 1))))));
It is working but I feel that this is not very elegant because I need to query the database to get the hierarchy of group prior to dynamically build the query in order to add as many a_group_librarydirectory_canaccess join and where clause as there are levels in the group hierarchy.
Is there a smarter way?
Thank you for your time

Related

Select from SQL database information with newest revisions

I coding web app for my client and have issue with selecting from database raports with newest revisions.
SELECT
raports.*,
r1.*,
users.*,
(SELECT COUNT(*) FROM changes WHERE changes.changes_raports_id = raports.raports_id) as changes,
(SELECT changes.changes_date FROM changes WHERE changes.changes_raports_id = raports.raports_id ORDER BY changes.changes_date DESC LIMIT 1) as last_change,
(SUM(injuries.injuries_min_procent) / COUNT(injuries_to_raports.injuries_to_raports_id)) as min,
(SUM(injuries.injuries_max_procent) / COUNT(injuries_to_raports.injuries_to_raports_id)) as max
FROM raports
LEFT JOIN users
ON users.users_id = raports.raports_users_id
LEFT JOIN changes
ON changes.changes_raports_id = raports.raports_id
LEFT JOIN raports_to_changes r1
ON r1.raports_to_changes_raports_id = raports.raports_id
LEFT JOIN injuries_to_raports
ON injuries_to_raports.injuries_to_raports_raports_id = r1.raports_to_changes_raports_id
LEFT JOIN injuries
ON injuries_to_raports.injuries_to_raports_injuries_id = injuries.injuries_id
WHERE r1.raports_to_changes_changes_id = (SELECT max(raports_to_changes_changes_id) FROM raports_to_changes r2 WHERE r2.raports_to_changes_raports_id = r1.raports_to_changes_raports_id)
GROUP BY raports.raports_id ORDER BY raports.raports_id ASC;
In columns max and min i have not correct average from injuries. When i checked it and count all injuries i had 36 when true number is 2 but i have 18 revisions. So is logic that i have looped COUNT with all revisions but i want only the newest
I try changing WHERE statements and more LEFT JOINs but nothing helped.
Could someone fixed that code?
Thank you in advanced
Based on the clues revealed by your queries, the data model may look like this:
The select list shows that you need:
users information of a reports_id
aggregated injuries_min_procent and injuries_max_procent at raports_id level. (see cte_raport_injuries)
number of changes of a raports_id (see cte_raport_changes)
the last change_date of a raports_id (see cte_raport_changes)
I'm not sure about the need for raports_of_changes based on information revealed in the question, so I'm going to ignore it for now.
with cte_raport_injuries as (
select r.raports_id,
sum(i.injuries_min_procent) / count(*) as injuries_min_procent,
sum(i.injuries_max_procent) / count(*) as injuries_max_procent
from raports r
join injuries_to_raports ir
on r.raports_id = ir.injuries_to_raports_raports_id
join injuries i
on ir.injuries_to_raports_injuries_id = i.injuries_id
group by r.raports_id),
cte_raport_changes as (
select r.raports_id,
count(c.changes_id) as changes,
max(c.changes_date) as last_change
from raports r
join changes c
on r.raports_id = c.changes_raports_id
group by r.raports_id)
select u.users_id,
r.raports_id,
ri.injuries_min_procent,
ri.injuries_max_procent,
rc.changes,
rc.last_change
from raports r
join users u
on r.raports_users_id = u.users_id
join cte_raport_injuries ri
on r.raports_id = ri.raports_id
join cte_raport_changes rc
on r.raports_id = rc.raports_id;
The result looks like this:
users_id|raports_id|injuries_min_procent|injuries_max_procent|changes|last_change|
--------+----------+--------------------+--------------------+-------+-----------+
1| 11| 15.0000| 25.0000| 2| 2022-12-02|
So my question for you is what's in reports_to_changes that you need and what's its relationship between others? For further involvement from the community, you may want to share the following information in text format:
DDLs of each tables (primary key, foreign key, column names & data types)
Some representable sample data and basic business rules
Expected output

Beginner SQL: JOIN clause skewing results of query

thank you all for taking the time to read and help if you can! I have a query below that is getting large and messy, I was hoping someone could point me in the right direction as I am still a beginner.
SELECT
DATE(s.created_time_stamp) AS Date,
s.security_profile_id AS Name,
COUNT(*) AS logins,
CASE
WHEN COUNT(s.security_profile_id) <= 1
THEN '1'
WHEN COUNT(s.security_profile_id) BETWEEN 2 AND 3
THEN '2-3'
ELSE '4+'
END AS sessions_summary
FROM session AS s
INNER JOIN member AS m
ON s.security_profile_id = m.security_profile_id
JOIN member_entitlement AS me ON m.id = me.member_id
JOIN member_package AS mp ON me.id = mp.member_entitlement_id
**JOIN member_channels AS mc ON mc.member_id = m.id**
where member_status = 'ACTIVE'
and metrix_exempt = 0
and m.created_time_stamp >= STR_TO_DATE('03/08/2022', '%m/%d/%Y')
and display_name not like 'john%doe%'
and email not like '%#aeturnum.com'
and email not like '%#trendertag.com'
and email not like '%#sargentlabs.com'
and member_email_status = 'ACTIVE'
and mp.package_id = 'ca972458-bc43-4822-a311-2d18bad2be96'
and display_name IS NOT NULL
and s.security_profile_id IS NOT NULL
**and mc.id IS NOT NULL**
GROUP BY
DATE(created_time_stamp),
Name
ORDER BY
DATE(created_time_stamp),
Name
The two parts of the query with asterisks are the two most recently added clauses and they skew the data. Without these, the query runs fine. I am trying get a session summary which works fine, but I only want the sessions of people who have a 'channel' created. Maybe mc.id IS NOT NULL is not the way to do this. I will share my query that shows me how many people have created channels. Essentially, I am trying to combine these two queries in the cleanest way possible. Any advice is greatly appreciated!
-- Users that have Topic Channels and Finished Set Up FOR TRIAL DASH**
select count(distinct(m.id)) AS created_topic_channel
from member m right join member_channels mc on mc.member_id = m.id
left join channels c on c.id = mc.channels_id
JOIN member_entitlement AS me ON m.id = me.member_id
JOIN member_package AS mp ON me.id = mp.member_entitlement_id
where title not like '# Mentions'
and member_status = 'ACTIVE'
and metrix_exempt = 0
and m.created_time_stamp >= STR_TO_DATE('03/08/2022', '%m/%d/%Y')
and display_name not like 'john%doe%'
and email not like '%#aeturnum.com'
and email not like '%#trendertag.com'
and email not like '%#sargentlabs.com'
and member_email_status = 'ACTIVE'
and display_name IS NOT NULL
and mp.package_id = 'ca972458-bc43-4822-a311-2d18bad2be96';
The metric I am trying to retrieve from the DB is how many users have created a channel and logged in at least twice. Thank you again and have a wonderful day!!
If id is the primary key of member_channels then it does not make sense to check if it is null.
If all you want is to check whether a member has a 'channel' created, then instead of the additional join to member_channels, which may cause the query to return more rows than expected, you could use EXISTS in the WHERE clause:
where member_status = 'ACTIVE'
and .......................
and EXISTS (SELECT 1 FROM member_channels AS mc WHERE mc.member_id = m.id)
I would guess your tables aren't at the same level of granularity. A member may have many sessions, and 0-many channels.
eg if member 123 has five sessions and creates three channels => 15 rows of data in this join.
To adjust for this, it's best practice to join on the same level of granularity. You could roll up sessions to the member level, channels to the member level, and then join both against members.

mySql query - web file sharing program

Here is my code. It is supposed to select the name of the person "to_whom" I'm sharing a particular file,
and the whole thing needs to return back the user_id (u_c.to_whom) and the user_name,
so I can populate my friends list with a checkboxe next to each other, that once clicked,
will share (save into a DB table) that particular file to a specific person.
This is from a web-app where users share files among each other.
SELECT u_c.to_whom, u.user_name
FROM files f
LEFT JOIN users_connections u_c
ON (u_c.who = :user_id OR u_c.to_whom = :user_id) AND u_c.friends = "Y"
LEFT JOIN users u
ON u.user_id = u_c.to_whom
WHERE f.file_id = 90
In the table files we have
file_id, file_name, file_desc, etc...
In the table users connections
id, who, to_whom, friends
1 1 4 Y
meaning that user 1 had initiated a friendship to user 4 and "Y" means the friendship is TRUE (N = still pending)
And in users we have
user_id, user_name
1 Jack
I cannot seem to get this working for some reason.
Can any advanced user help me out?
Thanks a bunch!!
I found the answer to this code. It is an essential code if you want to build a sharing file system.
$query_list_count = "SELECT COUNT(user_id)
FROM users_connections u_c
LEFT JOIN users u
ON u.user_id = u_c.who OR u.user_id = u_c.to_whom
LEFT JOIN files f
ON f.file_id = :file_id
WHERE (u_c.who = :user_id OR u_c.to_whom = :user_id AND friends = :friends)
GROUP BY u.user_id
";
$result_list_count = $db->prepare($query_list_count);
$result_list_count->bindValue(':user_id', $_SESSION['user_id'], PDO::PARAM_INT);
$result_list_count->bindValue(':friends', "Y", PDO::PARAM_STR);
$result_list_count->bindValue(':file_id', $file_id, PDO::PARAM_INT);
$count_ = $result_list_count->fetchColumn();

Trying to building optimised Query for Group and Subgroup for a user

i am trying to write the Query for three things .My table structure is like that
You can see Schema at http://sqlfiddle.com/#!2/56c2d/1
I am trying to write the query in MYSQL
user:- table
user_id
user_fname
This is User tabke which will save User Information
group:- "group" and "subgroup" is maintain in same table using column "group_parent_group_id"
group_id
group_title
group_parent_group_id(INT)
This is group table which will save Group and Subgroups
user_group: table
user_group_id
user_group_user_id
user_group_group_id
This ill store both User and Group relation using their Id
I am trying to write the Query for three things. Fetching Users Groups, Subgroups
1) Query to fetch list of All Groups for User Register. Query is gelow and is giving error
Query:
select user.id, user.user_fname, group.group_id, group.group_title
from `user`
inner join user_group on user_group.user_group_user_id = user.user_id
inner join group on group.group_id = user_group.user_group_group_id
where user_group.user_group_user_id = 1 and user_group.group_parent_group_id = 0
2) I am Looking the query to fetch all subgroups(For Whom user is already Register) for Group Id 1,2 or 1
3) I am Looking the query to fetch all subgroups(For Whom user is Not Register yet) for Group Id 1,2 or 1. Ideal is for giving him randomly suggest a subgroup to add
Please Help. I am a newbie in DB :(
Your query is probably failing as you have a table called group, which is a reserved word. You can use back tics to delimit the name to get away with this (as follows) but it would be a better idea to change the table name.
SELECT user.id, user.user_fname, `group`.group_id, `group`.group_title
FROM `user`
INNER JOIN user_group ON user_group.user_group_user_id = user.user_id
INNER JOIN `group` ON `group`.group_id = user_group.user_group_group_id
WHERE user_group.user_group_user_id = 1
AND user_group.group_parent_group_id = 0
EDIT updated for queries I think the OP requires.
First query will get a list of all the groups (ones that have no parent group id) that a user (in this case id 28) is a member of
SELECT y2m_user.user_id, y2m_user.user_first_name, y2m_group.group_id, y2m_group.group_title
FROM y2m_user
INNER JOIN y2m_user_group ON y2m_user_group.user_group_user_id = y2m_user.user_id
INNER JOIN y2m_group ON y2m_group.group_id = y2m_user_group.user_group_group_id
WHERE y2m_user.user_id = 28
AND y2m_group.group_parent_group_id = 0
This query will get a list of all the sub groups (ones where the parent group id is greater than 0) that a user (in this case id 28) is a member of
SELECT y2m_user.user_id, y2m_user.user_first_name, y2m_group.group_id, y2m_group.group_title
FROM y2m_user
INNER JOIN y2m_user_group ON y2m_user_group.user_group_user_id = y2m_user.user_id
INNER JOIN y2m_group ON y2m_group.group_id = y2m_user_group.user_group_group_id
WHERE y2m_user.user_id = 28
AND y2m_group.group_parent_group_id > 0
This query will get a list of all the sub groups (ones where the parent group id is greater than 0) that a user (in this case id 28) is NOT a member of
SELECT y2m_user.user_id, y2m_user.user_first_name, y2m_group.group_id, y2m_group.group_title
FROM y2m_user
CROSS JOIN y2m_group
LEFT OUTER JOIN y2m_user_group ON y2m_user_group.user_group_user_id = y2m_user.user_id AND y2m_group.group_id = y2m_user_group.user_group_group_id
WHERE y2m_user.user_id = 28
AND y2m_group.group_parent_group_id > 0
AND y2m_user_group.user_group_id IS NULL
Please excuse any typos as not tested (with your test data there are no sub groups).

SQL Left Join Problem

I'm creating a checklist-style program, where files are assigned to checklists, and you check items off of checklists for certain files. I'm trying to run a query that returns all files that are READY for a certain checklist item (items are ordered).
So for example, I am trying to see which files are ready for checklist item number 3, so I need to find all files that have been marked as checked for item number 2, BUT NOT for item number 3.
I would also prefer to NOT use sub-queries, even though I know sub-queries would solve this problem (that's why this is taking so long), since this query will be run for every checklist item, and I feel that sub-queries here would affect performance negatively.
Here is my query so far:
SELECT
DISTINCT f.filename, f.id
FROM
files f LEFT JOIN checklist_item_checklist cic1 LEFT JOIN checklist_check cc1 ON
cc1.checklist_item_checklist_id = cic1.checklist_item_checklist_id ON
cc1.file_id != f.id,checklist_item_checklist cic2,
checklist_check cc2
WHERE
cic1.checklist_item_checklist_id = 2 AND
cic2.order_number = cic1.order_number - 1 AND
cic1.checklist_id = cic2.checklist_id AND
cc2.checklist_item_checklist_id = cic2.checklist_item_checklist_id AND
cc2.file_id = f.id
The table structure is:
files
id (PK)
filename
checklist_item_checklist
checklist_item_checklist_id (PK)
order_number
checklist_check
file_id (FK to files.id)
checklist_item_checklist_id (FK to checklist_item_checklist.checklist_item_checklist_id)
Thanks!
You have lots of syntax errors in your query. I believe you need a subquery.
This should work:
SELECT f.id, f.filename
FROM files f
JOIN checklist_check cc ON cc.file_id = f.id
JOIN checklist_item_checklist cic ON cic.checklist_item_checklist_id = cc.checklist_item_checklist_id AND cc.order_number = 2
WHERE f.id not in(
SELECT f.id,
FROM files f
JOIN checklist_check cc ON cc.file_id = f.id
JOIN checklist_item_checklist cic ON cic.checklist_item_checklist_id = cc.checklist_item_checklist_id AND cc.order_number = 3)