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

  • Home
  • SEARCH
  • 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 7511069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:26:15+00:00 2026-05-29T23:26:15+00:00

I have a problem with getting some ordering of records and uniqueness. I have

  • 0

I have a problem with getting some ordering of records and uniqueness. I have the following setup of tables:

person
groups
group_has_person

The idea here is to sort persons into groups. (think of Google+ circles)

First I had only a person table with 2 ID fields linking to the group records but then I was always limited to having a person only in 2 groups. Now I have added a linking table so the person can be placed into an unlimited amount of groups. Every group has a sort index field (group.order_index). This is an integer defining the sort-order of the groups. The idea is to show all persons grouped by group and showing every person only once but also order not by name or id but but by the extra index field of the group. So actually I want to show all persons even when they’re not in a group. But when they are in one or more groups I want to show them in the order of the group.order_index and only once.

The query result should look like this:

person.name   person.person_id   group.group_id   group.order_index 
Rick          1                  1                1
Tom           2                  1                1
Jan           4                  3                2
Kees          3                  3                2
Piet          5                  NULL             NULL

Notice that the group_id is not the column to order by but that table has an extra field so the order can be changed after creation of a group.

I got some queries that got close to my expected result but I’m not there yet:

This following query gives the expected sort result but it still gives a person multiple times when it is in multiple groups:

SELECT
  person.person_id AS 'person.person_id',
  person.name AS 'person.name',
  group.order_index AS 'group.order_index',
FROM
  `person`
LEFT OUTER JOIN `group_has_person` ON person.person_id = group_has_person.person_id
LEFT OUTER JOIN `group` ON group_has_person.group_id = group.group_id
ORDER BY
  3 ASC , 
  2 ASC

The key-word DISTINCT didn’t help either.

The following query gives unique persons but the order by group_order_index doesn’t work and even shows faulty numbers:

SELECT
  person.person_id AS 'person.person_id',
  person.name AS 'person.name',
  group.order_index AS 'group.order_index',
FROM
  `person`
LEFT OUTER JOIN `group_has_person` ON person.person_id = group_has_person.person_id
LEFT OUTER JOIN `group` ON group_has_person.group_id = group.workgroup_id
GROUP BY
  group_has_person.person_id
ORDER BY
  3 ASC , 
  2 ASC

Also grouping by person.person_id doesn’t help getting the right order.

Inner joins also don’t work because they wont show persons that are not in a group. Is it possible to get what I want without code and only by a query without having sub-selects?

  • 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-29T23:26:17+00:00Added an answer on May 29, 2026 at 11:26 pm

    You are almost there, but you need an aggregate function on order_index:

    SELECT
    person.person_id AS 'person.person_id',
    person.name AS 'person.name',
    MIN(group.order_index) AS 'group.order_index',
    FROM person
    LEFT OUTER JOIN group_has_person ON person.person_id = group_has_person.person_id
    LEFT OUTER JOIN group ON group_has_person.group_id = group.workgroup_id
    GROUP BY
    person.person_id, person.name
    ORDER BY
    3 ASC, 2 ASC
    

    In other words, if a person belongs to several groups, you can’t avoid duplicates unless you pick which order_index to sort by using min, max, avg or sum.

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

Sidebar

Related Questions

I have a problem with getting my pager-function to work. For some reason it
i am getting problem as i have some data in a database tabel file
I have a problem with getting some html value stored in a php array:
I have a problem getting the following scenario to work. A student can take
I have problem with getting value of parameter when I have variable with some
I have no problem getting a value from a text box with some id:
Framework Scrapy - Scrapyd server. I have some problem with getting jobid value inside
I have got the following scenario. In my SL application, I am getting some
I have a problem in getting some data out from Cassandra using c# and
I'm trying to do an asynchronous request with ASIHTTPRequest, but have some problem getting

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.