I am working on a query whose purpose is to get the records of all the students whose financialyear_id!=4 and don't dispaly records even if he/she has finacialyear_id other then 4 exist.
I have written a query but it gives me the record of that student whose finacialyear_id!=4 but I want to achieve that no records will be shown if financialyear_id=4 exist for any student.
SELECT a.id aid
, s.id sid
, s.name
, s.father_name
, s.cnic
, f.financialyear_id
FROM student s
JOIN academic_info a
ON a.s_id = s.id
LEFT
JOIN fee_issued f
ON a.id = f.academic_info_id
WHERE f.financialyear_id != 4
AND a.is_data_locked = 0
AND a.university_id = 60;
Foreign Key: s_id in both tables academic_info and fee_issued,academic_info_id in fee_issued table.
You used a keyword in your request: "but I want to achieve that no records will be shown if financialyear_id=4 exist for any student." So use EXISTS (or IN which does about the same) to check for existence.
As you are using MySQL you must write the select condition for academic_info twice. Other DBMS handle this more elegantly.
select a.id as aid, s.id as sid, s.name, s.father_name, s.cnic, f.financialyear_id
from student s
join academic_info a on a.s_id = s.id and a.is_data_locked = 0 and a.university_id = 60
left join fee_issued f on f.academic_info_id = a.id
where s.id not in
(
select ai.s_id
from academic_info ai
join fee_issued fi on fi.academic_info_id = ai.id and fi.financialyear_id != 4
where ai.is_data_locked = 0 and ai.university_id = 60
);
Above query also gets you students that have no fee_issued at all. If you want these removed, change the left join to an inner join.
EDIT: Here is the same with NOT EXISTS.
select a.id as aid, s.id as sid, s.name, s.father_name, s.cnic, f.financialyear_id
from student s
join academic_info a on a.s_id = s.id and a.is_data_locked = 0 and a.university_id = 60
left join fee_issued f on f.academic_info_id = a.id
where not exists
(
select *
from academic_info ai
join fee_issued fi on fi.academic_info_id = ai.id and fi.financialyear_id != 4
where ai.is_data_locked = 0 and ai.university_id = 60
and ai.s_id = s.id
);
Related
I have the following query in MySQL:
(SELECT ue.id, ue.userid, ue.status, ue.timestart, ue.timeend, e.courseid,
e.id AS enrolid, ra.roleid
FROM user_enrolments ue
JOIN enrol e ON e.id = ue.enrolid
JOIN course c ON c.id = e.courseid
JOIN user u ON u.id = ue.userid
JOIN context ct ON ct.instanceid = c.id
LEFT JOIN role_assignments ra ON ra.userid = u.id AND
ra.contextid = ct.id AND
ra.itemid = e.id
WHERE e.customint1 = 1 AND u.deleted = 0 AND
ct.contextlevel = 50 AND (ue.status = 0 OR ue.status = 1))
UNION
(SELECT de.enrolid AS id, de.userid, de.status, de.date_ini, de.date_fin,
de.courseid, de.enrolid, de.roleid
FROM deleted_enrols de
JOIN user u ON u.id = de.userid
WHERE userid = ANY (SELECT userid FROM local_users WHERE clientid = 1))
ORDER BY u.firstname, u.lastname, c.fullname LIMIT 0, 100
If I delete ORBER BY and LIMIT, this query works fine... but the ORDER BY clause gives an error:
Table 'u' from one of the SELECTs cannot be used in global ORDER clause
If I delete the parentheses of both SELECT querys, the error is different:
Table 'u' from one of the SELECTs cannot be used in field list
I have also tried with UNION ALL, but it does not work either.
Any suggestion or clue? Thanks in advance for your time...
The results of your UNION do not include any fields from table 'u', so those results cannot be sorted by table 'u' fields.
You could perhaps perform the UNION and then re-join the results to table 'u', and then use that to sort the results by table 'u' fields. A similar issue exists for sorting on
course.fullname, so that would need to be joined back in, too.
SELECT x.id, x.userid, x.status, x.timestart, x.timeend, x.courseid, x.enrolid, x.roleid
FROM ((SELECT ue.id, ue.userid, ue.status, ue.timestart, ue.timeend, e.courseid,
e.id AS enrolid, ra.roleid
FROM user_enrolments ue
JOIN enrol e ON e.id = ue.enrolid
JOIN course c ON c.id = e.courseid
JOIN user u ON u.id = ue.userid
JOIN context ct ON ct.instanceid = c.id
LEFT JOIN role_assignments ra ON ra.userid = u.id
AND ra.contextid = ct.id
AND ra.itemid = e.id
WHERE e.customint1 = 1 AND u.deleted = 0
AND ct.contextlevel = 50 AND (ue.status = 0 OR ue.status = 1))
UNION
(SELECT de.enrolid AS id, de.userid, de.status, de.date_ini, de.date_fin,
de.courseid, de.enrolid, de.roleid
FROM deleted_enrols de
JOIN user u ON u.id = de.userid
WHERE userid = ANY (SELECT userid FROM local_users WHERE clientid = 1))
) x
JOIN user z ON z.id = x.userid
JOIN course d ON d.id = x.courseid
ORDER BY z.firstname, z.lastname, d.fullname LIMIT 0, 100
Assuming you want to sort the whole lot, try parentheses round the whole query with the ORDER BY done afterwards:
select id, userid, status, timestart, timeend, courseid, enrolid, roleid from
((SELECT ue.id, ue.userid, ue.status, ue.timestart, ue.timeend, e.courseid,
e.id AS enrolid, ra.roleid, u.firstname, u.lastname, c.fullname
FROM user_enrolments ue
JOIN enrol e ON e.id = ue.enrolid
JOIN course c ON c.id = e.courseid
JOIN user u ON u.id = ue.userid
JOIN context ct ON ct.instanceid = c.id
LEFT JOIN role_assignments ra ON ra.userid = u.id AND
ra.contextid = ct.id AND
ra.itemid = e.id
WHERE e.customint1 = 1 AND u.deleted = 0 AND
ct.contextlevel = 50 AND (ue.status = 0 OR ue.status = 1))
UNION
(SELECT de.enrolid AS id, de.userid, de.status, de.date_ini, de.date_fin,
de.courseid, de.enrolid, de.roleid, u.firstname, u.lastname, ' ' as fullname
FROM deleted_enrols de
JOIN user u ON u.id = de.userid
WHERE userid = ANY (SELECT userid FROM local_users WHERE clientid = 1))) s1
ORDER BY firstname, lastname, fullname LIMIT 0, 100
(obviously fullname in the second SELECT statement would be populated however seems sensible)
You need to include the data to be ordered by in the selects of the unioned queries; an ORDER BY following a UNION is handled as if it were SELECT * FROM (unions) ORDER BY ... so anything not coming out of the union cannot be used for ordering.
Ironically, a query similar to that is the key to getting what you want though, with something like
SELECT x, y, z
FROM (
SELECT x, y, z, somethingIdontactuallywant
FROM blah
UNION
SELECT a, b, c, somethingIdontactuallywant
FROM blah2
) AS u
ORDER BY u.somethingIdontactuallywant
As mysql documentation on union says:
This kind of ORDER BY cannot use column references that include a
table name (that is, names in tbl_name.col_name format). Instead,
provide a column alias in the first SELECT statement and refer to the
alias in the ORDER BY. (Alternatively, refer to the column in the
ORDER BY using its column position. However, use of column positions
is deprecated.)
Also, if a column to be sorted is aliased, the ORDER BY clause must
refer to the alias, not the column name.
So, do not refer to any table names and use columns that are actually in the resultset of the union.
Hy everyone,
I really need your help. I have to obtain the list (distinct) of all users who :
- have a live checkin ( checkins.ctype = 'live' ) in a match
- where they favorite team ( see fanusers_teams )
- won by 3 ore more goals difference.
The favorite team, could be info_matches.team_id1 OR info_matches.team_id2 or even both.
Here is a small design for the involved tables :
What I've tried, works 80% (so it doesn't :( ) because it returns some users correct (they fav. teams by 3+ goals diff) , but also returns users which don't have a fav. team in the situation. I think that they are returned because they've made a live checkin for a match where one team or the other has won by a 3+ goals diff.
Here is my query :
SELECT DISTINCT
f.id
FROM
fanusers f
LEFT JOIN
checkins c ON f.id = c.fanuser_id
LEFT JOIN
info_matches m ON m.id = c.match_id
WHERE
c.ctype = 'live' AND
(
m.team_id1 IN(
SELECT DISTINCT
m1.team_id1
FROM
info_matches m1
RIGHT JOIN
fanusers_teams ft ON m1.team_id1 = ft.team_id
RIGHT JOIN
fanusers f ON f.id = ft.fanuser_id
WHERE
m1.pointsteam1 - m1.pointsteam2 >= 3
)
OR
m.team_id2 IN(
SELECT DISTINCT
m2.team_id2
FROM
info_matches m2
RIGHT JOIN
fanusers_teams ft ON m2.team_id2 = ft.team_id
RIGHT JOIN
fanusers f ON f.id = ft.fanuser_id
WHERE
m2.pointsteam2 - m2.pointsteam1 >= 3
)
)
I would appreciate also a small explanation regarding what am I doing wrong, if there is someone who succeed to solve this query.
Thanks.
Something like this should work:
SELECT DISTINCT f.id
FROM fanusers f
JOIN checkins c
ON f.id = c.fanuser_id
JOIN fanusers_teams ft
ON f.id = ft.fanuser_id
JOIN info_matches m
ON m.id = c.match_id
AND
(
(ft.team_id = m.team_id1 AND pointsteam1 - pointsteam2 >= 3)
OR
(ft.team_id = m.team_id2 AND pointsteam2 - pointsteam1 >= 3)
)
WHERE c.ctype = 'live'
I've found also a way to solve the query, but not so nice as Tom did it.
My way :
SELECT DISTINCT
f.id AS user_id
FROM
fanusers f
LEFT JOIN
checkins c ON f.id = c.fanuser_id
LEFT JOIN
info_matches m ON m.id = c.match_id
WHERE
c.ctype = 'live'
AND
m.matchisfinished = 1
AND
c.fanuser_id NOT IN ( SELECT DISTINCT
f1.id
FROM
fanusers f1
LEFT JOIN
fanusers_stickers fs
ON f1.id = fs.fanuser_id
WHERE
fs.sticker_id = 35
)
AND
(
( m.team_id1 IN ( SELECT DISTINCT
ft.team_id
FROM
fanusers_teams ft
INNER JOIN
checkins c1
ON
ft.fanuser_id = c1.fanuser_id
WHERE
f.id = c1.fanuser_id
)
AND
m.pointsteam1-m.pointsteam2>=3
)
OR
(
m.team_id2 IN ( SELECT DISTINCT
ft.team_id
FROM
fanusers_teams ft
INNER JOIN
checkins c1
ON
ft.fanuser_id = c1.fanuser_id
WHERE
f.id = c1.fanuser_id
)
AND
m.pointsteam2-m.pointsteam1>=3
)
)
So for the moment, even if I'm happy that I've solved the query by myself, I will use Tom's query :).
I'm still fairly new to SQL and I'm not fully understanding where the problem in my code is coming from. The code below mostly comes from my work so I didn't write it from scratch. The code gathers a bunch of different information and filters based on it. If you look in the code you'll see that a student has many observations_students which it is related to. The first version of the code returns the information of all students who have an observations_student with observation_id = 2567. This seems to work correctly with the following code:
SELECT DISTINCT
SUBSTRING(s.osis_id,INSTR(s.osis_id,'-')+1) AS osid,
s.id AS student_id,
CONCAT(s.last_name, ' ',s.first_name) AS sname
FROM students s
# course info
INNER JOIN
(
SELECT c.id AS cid,
c.description AS cname,
cs.date_end,
cs.student_id,
gl.description AS grade,
c.gradelevel_id
FROM courses_students cs
INNER JOIN courses c ON c.id = cs.course_id
INNER JOIN gradelevels gl ON gl.id = c.gradelevel_id
WHERE
IFNULL(cs.date_end, NOW()) >= NOW()
AND IFNULL(c.date_end, NOW()) >= NOW()
AND c.school_id = 1509
AND c.subject_id = 24
) AS cs ON cs.student_id = s.id
# RTI flag info
INNER JOIN
(
SELECT os.id,
os.student_id
FROM observations o
INNER JOIN observations_students os ON os.observation_id = 2567
WHERE
o.school_id = 1509
) AS os ON os.student_id = s.id
LEFT JOIN schools_students ss ON ss.student_id = s.id
WHERE s.active = 1
AND ss.school_id = 1509
AND IFNULL(ss.date_end,NOW()) >= NOW()
AND cs.gradelevel_id BETWEEN 10 AND 16
What I would like to do after this is for each of these students whom have the 2567 observation, I would like to find the number of 2009 observations that student has. To do this, I am adding another LEFT JOIN and the completed code looks as follows:
SELECT DISTINCT
SUBSTRING(s.osis_id,INSTR(s.osis_id,'-')+1) AS osid,
s.id AS student_id,
CONCAT(s.last_name, ' ',s.first_name) AS sname,
COUNT(fdos.id) AS fd_count
FROM students s
# course info
INNER JOIN
(
SELECT c.id AS cid,
c.description AS cname,
cs.date_end,
cs.student_id,
gl.description AS grade,
c.gradelevel_id
FROM courses_students cs
INNER JOIN courses c ON c.id = cs.course_id
INNER JOIN gradelevels gl ON gl.id = c.gradelevel_id
WHERE
IFNULL(cs.date_end, NOW()) >= NOW()
AND IFNULL(c.date_end, NOW()) >= NOW()
AND c.school_id = 1509
AND c.subject_id = 24
) AS cs ON cs.student_id = s.id
# RTI flag info
INNER JOIN
(
SELECT os.id,
os.student_id
FROM observations o
INNER JOIN observations_students os ON os.observation_id = 2567
WHERE
o.school_id = 1509
) AS os ON os.student_id = s.id
LEFT JOIN
(
SELECT fdos.id,
fdos.student_id
FROM observations o
INNER JOIN observations_students fdos ON fdos.observation_id = 2009
WHERE
o.school_id = 1509
) AS fdos ON fdos.student_id = s.id
LEFT JOIN schools_students ss ON ss.student_id = s.id
WHERE s.active = 1
AND ss.school_id = 1509
AND IFNULL(ss.date_end,NOW()) >= NOW()
AND cs.gradelevel_id BETWEEN 10 AND 16
If I change the "COUNT(fdos.id) AS fd_count" to "fdos.id AS fdosid" I am returned the correct number of entries. However, the number returned from the COUNT is not the same number and is not correct. Can anyone understand what's going on here well enough to explain what I'm doing wrong?
Thank you for your time.
I can bet you're using MySQL.
If you use anything of:
GROUP BY clause;
HAVING clause;
aggregate function, which count() is
then your query is being aggregated one.
This means, that data will be groupped by the fields specified in the GROUP BY clause, such fields should be kept as-is in the select list and elsewhere in the query. All other fields should be arguments of the aggregate functions, otherwise database has no clue which value from the set that matches your group it should return.
All major databases will give you an error for a query contructed the way you did, as there's no GROUP BY clause for a bunch of fields: s.osis_id, s.id, s.last_name and s.first_name. MySQL will not. Instead, it will implicitly group data. I don't know what is the grouping criteria, and I don't want to, as this behaviour is error prone and unreliable.
Instead, your query should be rewritten.
The easiest way is to:
use your existing query without the count() function, i.e. get a list of fdos.id;
use the whole query as another subquery, omiting the DISTINCT clause;
counting the students.
Something like this:
SELECT osid, student_id, sname, count(fdos_id) AS fd_count
FROM (
SELECT
substring(s.osis_id,instr(s.osis_id,'-')+1) AS osid,
s.id AS student_id,
concat(s.last_name, ' ',s.first_name) AS sname,
fdos.id AS fdos_id
FROM students s
...
) AS src
GROUP BY osid, student_id, sname
ORDER BY osid, student_id, sname;
seems the INNER JOIN of os has already filtered your result to show only observation_id = 2567. Therefore you cannot get any other records for different observation_id. You can change that INNER JOIN to LEFT JOIN and see how it's going.
SELECT DISTINCT
SUBSTRING(s.osis_id,INSTR(s.osis_id,'-')+1) AS osid,
s.id AS student_id,
CONCAT(s.last_name, ' ',s.first_name) AS sname,
COUNT(fdos.id) AS fd_count
FROM students s
# course info
INNER JOIN
(
SELECT c.id AS cid,
c.description AS cname,
cs.date_end,
cs.student_id,
gl.description AS grade,
c.gradelevel_id
FROM courses_students cs
INNER JOIN courses c ON c.id = cs.course_id
INNER JOIN gradelevels gl ON gl.id = c.gradelevel_id
WHERE
IFNULL(cs.date_end, NOW()) >= NOW()
AND IFNULL(c.date_end, NOW()) >= NOW()
AND c.school_id = 1509
AND c.subject_id = 24
) AS cs ON cs.student_id = s.id
# RTI flag info
LEFT JOIN #change this to LEFT JOIN
(
SELECT os.id,
os.student_id
FROM observations o
INNER JOIN observations_students os ON os.observation_id = 2567
WHERE
o.school_id = 1509
) AS os ON os.student_id = s.id
LEFT JOIN
(
SELECT fdos.id,
fdos.student_id
FROM observations o
INNER JOIN observations_students fdos ON fdos.observation_id = 2009
WHERE
o.school_id = 1509
) AS fdos ON fdos.student_id = s.id
LEFT JOIN schools_students ss ON ss.student_id = s.id
WHERE s.active = 1
AND ss.school_id = 1509
AND IFNULL(ss.date_end,NOW()) >= NOW()
AND cs.gradelevel_id BETWEEN 10 AND 16
A quick fix seems to be to change COUNT(fdos.id)to COUNT(*).
Here's an explanation. The fdos results are being outer-joined and therefore fdos rows may not be returned for some of the rows on the left side of the join. When they are not returned, the corresponding columns (including fdos.id) are returned as NULLs. But COUNT() omits NULLs, which means that COUNT(fdos.id) would omit certain rows of the join's result set. The standard way of counting all rows regardless of matches, NULLs etc. is with COUNT(*).
select
s.id, s.description, s.improvement, s.previous_year_id,
s.current_year_id, s.first_name, s.last_name, s.username,
s.finding, s.action, s.share, s.learned, s.timestamp,
d.title as department_title,
group_concat(g.title SEPARATOR \' | \') as strategic_goals,
y1.year as current_year_title, y2.year as previous_year_title,
u.summary_id, u.file_name as file_name
from
summary s, year y1, year y2, strategic_goal_entries sge,
goal g, department d, uploads u
where
s.id = sge.summary_id
and
s.current_year_id = y1.id
and
s.previous_year_id = y2.id
and
sge.goal_id = g.id
and
s.id = u.summary_id
and
s.department_id = d.id
and
s.department_id = '4'
group by
s.id
This only returns records from the summary table that has a relating record in the uploads table (s.id = uploads.summary_id) that contain a value within the uploads.summary_id field
I want to return all records, whether or not it has a file associated with it.
Any help is appreciated.
Suggest refactoring this SQL query to use ANSI joins. To achive your goal, you'd want a LEFT JOIN instead:
SELECT /*your columns*/
from summary s
INNER JOIN year y1 ON s.current_year_id = y1.id
INNER JOIN year y2 ON s.previous_year_id = y2.id
INNER JOIN strategic_goal_entries sge ON s.id = sge.summary_id
INNER JOIN goal g ON sge.goal_id = g.id
INNER JOIN department d ON s.department_id = d.id
LEFT JOIN uploads u ON s.id = u.summary_id
WHERE s.department_id = '4'
group by s.id
Just wondering what's a better way to write this query. Cheers.
SELECT r.user_id AS ID, m.prenom, m.nom
FROM `0_rank` AS l
LEFT JOIN `0_right` AS r ON r.rank_id = l.id
LEFT JOIN `0_user` AS m ON r.user_id = m.id
WHERE r.section_id = $section_id
AND l.rank = '$rank_name' AND depart_id IN
(SELECT depart_id FROM 0_depart WHERE user_id = $user_id AND section_id = $section_id)
GROUP BY r.user_id
Here are the table structures:
0_rank: id | section_id | rank_name |
other_stuffs
0_user: id | prenom | nom | other_stuffs
0_right: id | section_id | user_id |
rank_id | other_stuffs
0_depart: id | section_id | user_id | depart_id
| other_stuffs
The idea is to use the same in a function like:
public function usergroup($section_id,$rank_name,$user_id) {
// mysql query goes here to get a list of appropriate users
}
Update: I think I have not been able to express myself clearly earlier. Here is the most recent query that seems to be working.
SELECT m.id, m.prenom, m.nom,
CAST( GROUP_CONCAT( DISTINCT d.depart ) AS char ) AS deps,
CAST( GROUP_CONCAT( DISTINCT x.depart ) AS char ) AS depx
FROM `0_rank` AS l
LEFT JOIN `0_right` AS r ON r.rank_id = l.id
LEFT JOIN `0_member` AS m ON r.user_id = m.id
LEFT JOIN `0_depart` AS d ON m.id = d.user_id
LEFT JOIN `0_depart` AS x ON x.user_id = $user_id
WHERE r.section = $section_id
AND l.rank = '$rank_name'
GROUP BY r.user_id ORDER BY prenom, nom
Now I want to get only those result, where all entries of deps are present in entries in depx.
In other term, every user is associated with some departs. $user_id is also an user is associated with some departs.
I want to get those users whose departs are common to the departs of $user_id.
Cheers.
Update
I'm not sure without being able to see the data but I believe this query will give you the results you want the fastest.
SELECT m.id, m.prenom, m.nom,
CAST( GROUP_CONCAT( DISTINCT d.depart ) AS char ) AS deps,
FROM `0_rank` AS l
LEFT JOIN `0_right` AS r ON r.rank_id = l.id and r.user_id = $user_id
LEFT JOIN `0_member` AS m ON r.user_id = m.id
LEFT JOIN `0_depart` AS d ON m.id = d.user_id
WHERE r.section = $section_id
AND l.rank = '$rank_name'
GROUP BY r.user_id ORDER BY prenom, nom
Let me know if this works.
Try this:
(By converting the functionality of the IN (SELECT...) to an inner join, you get exactly the same results but it might be the optimizer will make better choices.)
SELECT r.user_id AS ID, m.prenom, m.nom
FROM `0_rank` AS l
LEFT JOIN `0_right` AS r ON r.rank_id = l.id and r.section_id = 2
LEFT JOIN `0_user` AS m ON r.user_id = m.id
INNER JOIN `0_depart` AS x ON l.section_id = x.section_id and x.user_id = $user_id AND x.section_id = $section_id
WHERE l.rank = 'mod'
GROUP BY r.user_id
I also moved the constraints on 0_right to the join statement because I think that is clearer -- presumably this change won't matter to the optimizer.
I know nothing about your DB structure but your subselect looks like it can be replaced with a simple INNER JOIN against whatever table has the depart column. MySQL is well known for its poor subquery optimization.
Without knowing the structures or indexes, I would first add "STRAIGHT_JOIN" if the critical criteria is in-fact from the 0-rank table. Then, ensure 0_rank has an index on "rank". Next, ensure the 0_right has an index on rank_id at a minimum, but rank_id, section to take advantage of BOTH your criteria. Index on 0_member on id.
Additionally, do you mean left-join (ie: record only required in the 0_rank or 0_member) on the respective 0_right and 0_member tables instead of a normal join (where BOTH tables must match on their IDs).
Finally, ensure index on the depart table on user_id.
SELECT STRAIGHT_JOIN
r.user_id AS ID,
m.prenom,
m.nom
FROM
0_rank AS l
LEFT JOIN `0_right` AS r
ON l.id = r.rank_id
AND r.section = 2
LEFT JOIN `0_member` AS m
ON r.user_id = m.id
WHERE
l.rank = 'mod'
AND depart IN (SELECT depart
FROM 0_depart
WHERE user_id = 2
AND user_sec = 2)
GROUP BY
r.user_id
---- revised post from feedback.
From the parameters you are listing, you are always including the User ID... If so, I would completely restructure it to get whatever info is for that user. Each user should apparently can be associated to multiple departments and may or may NOT match the given rank / department / section you are looking for... I would START the query with the ONE USER because THAT will guarantee a single entry, THEN tune-down to the other elements...
select STRAIGHT_JOIN
u.id,
u.prenom,
u.nom,
u.other_stuffs,
rank.rank_name
from
0_user u
left join 0_right r
on u.id = r.user_id
AND r.section_id = $section_id
join 0_rank rank
on r.rank_id = rank.id
AND rank.rank_name = '$rank_name'
left join 0_dept dept
on u.id = dept.user_id
where
u.id = $user_id
Additionally, I have concern about your table relationships and don't see a legit join to the department table...
0_user
0_right by User_ID
0_rank by right.rank_id
0_dept has section which could join to rank or right, but nothing to user_id directly
Run explain on the query - it will help you find where the caveats are:
EXPLAIN SELECT r.user_id AS ID, m.prenom, m.nom
FROM 0_rank AS l
LEFT JOIN `0_right` AS r ON r.rank_id = l.id
LEFT JOIN `0_member` AS m ON r.user_id = m.id
WHERE r.section = 2
AND l.rank = 'mod' AND depart IN
(SELECT depart FROM 0_depart WHERE user_id = 2 AND user_sec = 2)
GROUP BY r.user_id\G