Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4270672
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:20:18+00:00 2026-05-21T07:20:18+00:00

Just wondering what’s a better way to write this query. Cheers. SELECT r.user_id AS

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T07:20:18+00:00Added an answer on May 21, 2026 at 7:20 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering if this would be an acceptable way of terminating an object from
Just wondering if anyone can optimize this usortMonths() function to be better? Basically I
Just wondering what the correct way of structuring statements like this is? @products =
Just wondering if there is a way around this (or if it is even
Just wondering, what would the best way for pulling information from headings in a
just wondering if there is a way to reduce the amount of code needed
Just wondering the best way to replace in place matches on a string. value.replace(bob,
Just wondering is it possible to build CLI app that can be run from
Just wondering what the best way to access read only information form a Microsoft
just wondering where to put this macro. It's supposed to cause correctly labelled excel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.