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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:31:17+00:00 2026-06-03T21:31:17+00:00

I have a table similar to: ID | NAME | GROUP_ID 1 | rand_str

  • 0

I have a table similar to:

ID  |  NAME    |  GROUP_ID
1   | rand_str |  4
2   | rand_str |  2
3   | rand_str |  3
4   | rand_str |  0
5   | rand_str |  3
6   | rand_str |  1
7   | rand_str |  3
8   | rand_str |  3
9   | rand_str |  1
10  | rand_str |  0

ID is unique ID, name valie is not null and contains varchar value and group_id is a max 2 digit positive number.

Let’s say that the query returns this table.

What I want is, using PHP, show the results grouped by their group_id and if possible, ordering the group id by the most numbered to least numbered and “0” being always the last no matter how populated it is.

so end result:

group id 3 exists 3 times, so

  1. 1- id_3, id_5, id_7 and id_8 (group_id 3)
  2. 2- id_6, id_9 (group_id 1)
  3. 3- either group id 4 or 2 since both contain 1 item each
  4. 4- group_id 0, even though it has 2 items -> id_4, id_10

How can I achieve this?

Thank you.

  • 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-03T21:31:19+00:00Added an answer on June 3, 2026 at 9:31 pm

    Use a subquery to determine the count of each group (and therefore its order), then join that with your data to retrieve the records:

    SELECT my_table.* FROM my_table JOIN (
      SELECT   GROUP_ID,
               IF(GROUP_ID = 0, -1, COUNT(*)) AS rank
      FROM     my_table
      GROUP BY GROUP_ID
    ) AS t USING (GROUP_ID)
    ORDER BY t.rank DESC
    

    See it on sqlfiddle.

    You would then loop over the results in PHP, keeping track of the GROUP_ID of the last record and comparing with the current one to see whether you’re now in a new group:

    $last = null;
    echo "<table>";
    while ($row = $stmt->fetch()) {
      if ($row['GROUP_ID'] !== $last) {
        if ($last !== null) echo "</tr>";
        echo "<tr><th scope='row'>Group " . html_entities($row['GROUP_ID']) . "</th>";
        $last = $row['GROUP_ID'];
      }
      echo "<td>ID " . html_entities($row['ID']) . "</td>";
    }
    echo "</tr></table>";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table similar to this +--+-----+----+ |ID|Entry|Exit| +--+-----+----+ |18|32154|NULL| +--+-----+----+ |19|NULL |NULL|
I have a SqlServer2005 table Group similar to the following: Id (PK, int) Name
I have a database table with 3 columns: _id, name, groupName It looks similar
Say I have a table similar to: ID Name 1 Test 2 Contest 3
I have a table like this: group_id name -------- ---- 1 David 1 John
I have a table similar to: ID...NAME.....HTMLTEXT 1....Footer....`<b>test</b>` where each entry has some HTML
I have a route table similar to this // Allow browsing categories by name,
I have a table with the following columns: group_id, parent_id, name In this table
I have a table similar to the following: id key str_val date_val num_val 1
i have a table which looks similar to this <asp:TableRow><asp:TableCell>Question 1</asp:TableCell><asp:TableCell ID =Question1Text></asp:TableCell></asp:TableRow> <asp:TableRow><asp:TableCell

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.