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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:48:02+00:00 2026-05-23T16:48:02+00:00

I have 2 tables CP and CS structured like this. CP id tu su

  • 0

I have 2 tables CP and CS structured like this.

CP
    id    tu    su
    1     7     1
    2     7     2

.———————————.

CS
    id    st    cp
    1     8     1
    2     9     1
    3     9     2
    4     2     1

.——————————–.

I want to make a function get_cp($tu) to return the data like this.
get_cp(7) =

su
    '1'
        [0] = array('st'=>'8')
        [1] = array('st'=>'9')
        [2] = array('st'=>'2')
    '2'
        [0] = array('st'=>'2')

It seems like I could use GROUP_CONCAT to make a X deliminated string and then parse it…but I was wondering if there is a “better” way. Parsing strings seems like it is wasteful, it would be great if mySQL could just return the data like this.
Should I just use no GROUP at all, and then use php to group things together after I have a big list of duplicates?

Thanks! And please ask questions if your unsure what I’m asking.
Thanks!!

  • 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-23T16:48:02+00:00Added an answer on May 23, 2026 at 4:48 pm

    You can use GROUP_CONCAT(). Generally speaking, I think if you can do it on the DB side, you should.

    However, in this case, you are going to have to loop over the results to construct the array you described no matter what query you use.

    As such, I’d do the following query and build the grouping with PHP.

    $data = array();
    $results = mysql_query('SELECT st, cp FROM CS');
    
    while ($row = mysql_fetch_assoc($results)) {
      $data[$row['cp']][] = $row['st'];
    }
    

    Note: This is not ordered for performance. If you need them in a certain order, then add an ORDER BY.

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

Sidebar

Related Questions

I have 2 tables: posts tags Tags table is structured like this: post_id tag
I'm a bit confused on this. I have a data table structured like this:
I have a table structured like this: <table> <tr id=id1 class=parent></tr> <tr class=id1></tr> <tr
I have a couple of tables with similar structures like this: <table> <tbody> <tr>content</tr>
I have two tables like of this structure: content (content_id, content_type, user_id, time, comment_count)
I have tried several topics like this one: How to find missing data rows
If I have a table structure like this: Transaction [TransID, ...] Document [DocID, TransID,
I have a table structure with columns like this [ID] [Name] [ParentId] [ParentName] The
I currently have a table structure that looks something like this(some details omitted): ColumnName
I have this table structure and would like to map it using Fluent Hibernate

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.