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 9314025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:01:41+00:00 2026-06-19T02:01:41+00:00

I am working on a query that for now has worked well (I’m using

  • 0

I am working on a query that for now has worked well (I’m using input from frontend to generate the WHERE clause), but I want to be able to search on another parameter and this truly makes my mind boggle.

  1. Table groups contains id, name.
  2. Table persons is my table with the persons that I want to retrieve.
  3. Table group_memberships is my group<->person many-to-many relationship table. The columns are participant (person.id) and group (groups.id)

The retrieved result set’s last column is a concatenation of group_memberships but where “1, 2, 3” has been replaced with the groups’ real names via JOIN.

Here’s the query today:

SELECT
  `persons`.`id`,
  CONCAT_WS(\' \', `fname`, `lname`) AS `full_name`,
  `job_title`,
  `email`,
  `phone`,
  `participant_group_memberships`.`memberships`
FROM
  `persons`
  LEFT JOIN (SELECT
               `participant`, `name`,
               GROUP_CONCAT(`groups`.`name` ORDER BY `groups`.`name`
                            SEPARATOR  \', \') AS `memberships`
             FROM
               `group_memberships` JOIN `groups`
                ON (`groups`.`id` = `group_memberships`.`group`)
             GROUP BY `participant`) AS `participant_group_memberships`
  ON (`participant_group_memberships`.`participant` = `persons`.`id`)
WHERE TRUE . $where . '
ORDER BY `full_name`

If I add WHERE name = ‘search_query_group’ then the concatenated list only will show that group which is not what I want. I still want to show all the groups that person belongs to.

I also want to achieve this with 1 query, and avoid having to use IN(), if possible.

enter image description here

As you can see, the last column (generated with GROUP_CONCAT) will only show the group I searched on, not all the groups the person is member of.

  • 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-06-19T02:01:42+00:00Added an answer on June 19, 2026 at 2:01 am

    You could use a SUM and CASE statement in a HAVING clause in your subquery to include your search results. You would also need to change your join to the subquery to be an INNER JOIN. This will return all persons who are in a certain group and show all of the groups they are members of.

    SELECT  `persons`.`id`,
            CONCAT_WS(\' \', `fname`, `lname`) AS `full_name`,
            `job_title`,
            `email`,
            `phone`,
            `participant_group_memberships`.`memberships`
    
    FROM    `persons` 
    
            JOIN `pers_department`
            ON (`pers_department`.`pers_rid` = `persons`.`id`)
    
            JOIN 
            (SELECT `participant`, `name`,
                    GROUP_CONCAT(`groups`.`name` ORDER BY `groups`.`name`
                                SEPARATOR  \', \') AS `memberships`
             FROM   `group_memberships` 
                    JOIN `groups` ON (`groups`.`id` = `group_memberships`.`group`)
             GROUP BY `participant`
             HAVING SUM(CASE WHEN name = 'search_query_group' THEN 1 ELSE 0 END) > 0
            ) AS `participant_group_memberships`
            ON (`participant_group_memberships`.`participant` = `persons`.`id`)
    
    WHERE TRUE . $where . '
    
    ORDER BY `full_name`
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this working query that outputs URL and click counts from database. <?php
Here is the query that I am working on: SELECT `unitid`, `name` FROM apartmentunits
I'm working on creating a SQL query that will pull records from a table
I had a working FREETEXTTABLE query that searched for a @searchString. I now need
I have a working query that will return some results(records) from my database, like:
Okay so right now I'm working on a program that has a whole bunch
I have below a working query that needs to be simplified. The reason is
I am working on a query processor that reads in long lists of document
I am working on .aspx page that uses a t-sql query that uses a
I have inherited a somewhat messy query that I am working on refactoring to

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.