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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:49:16+00:00 2026-05-23T15:49:16+00:00

I have tables that look like this: questions: id description 1 Q1 2 Q2

  • 0

I have tables that look like this:

questions:
id description
1   Q1
2   Q2

answers:
id question_id x y description
1     1        1 2   A1
2     1        3 4   A2
3     2        5 6   A3
4     2        7 8   A4

What I want to get is a query that can output this:

Q1 A1 1,2 A2 3,4
Q2 A3 5,6 A4 7,8

I’ve been pulling my hair for days now trying to figure this out. I’m doing this in PHP and MySQL so if anyone can shed some light out there, that’d be really great.

EDIT: I forgot to mention that I’m using CodeIgniter for this, too. So, that might help with the answers.

  • 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-23T15:49:17+00:00Added an answer on May 23, 2026 at 3:49 pm

    Considering that there might be a random number of answers per question, you cannot design a query that will return a fixed number of columns. You have to return a single result per question and then do a little bit of parsing in your code.

    The GROUP_CONCAT function can be helpful for this kind of problem:

    SELECT q.description, GROUP_CONCAT(
        CONCAT(a.description,' ',a.x,',',a.y) ORDER BY a.id
        SEPARATOR ' '
        ) AS answers
    FROM questions q
      JOIN answers a ON a.question_id = q.id
    GROUP BY q.description;
    

    Will return

    +-------------+---------------+
    | description | answers       |
    +-------------+---------------+
    | Q1          | A1 1,2 A2 3,4 |
    | Q2          | A3 5,6 A4 7,8 |
    +-------------+---------------+
    2 rows in set (0.00 sec)
    

    You can change the SEPARATOR value by whatever you want to parse the result in your code. You can use the ORDER BY clause of the GROUP_CONCAT function to order the answers in the returned result for each answer (here I ordered by answer id).

    Edit: If you are sure that there will never be more than 4 answers per question, you can issue the following query to put each answer in its own column:

    SELECT description,
      REPLACE(SUBSTRING(SUBSTRING_INDEX(answers, '$', 1), LENGTH(SUBSTRING_INDEX(answers, '$', 1 - 1)) + 1), '$', '') answer_1,
      REPLACE(SUBSTRING(SUBSTRING_INDEX(answers, '$', 2), LENGTH(SUBSTRING_INDEX(answers, '$', 2 - 1)) + 1), '$', '') answer_2,
      REPLACE(SUBSTRING(SUBSTRING_INDEX(answers, '$', 3), LENGTH(SUBSTRING_INDEX(answers, '$', 3 - 1)) + 1), '$', '') answer_3,
      REPLACE(SUBSTRING(SUBSTRING_INDEX(answers, '$', 4), LENGTH(SUBSTRING_INDEX(answers, '$', 4 - 1)) + 1), '$', '') answer_4
    FROM (
        SELECT q.description, GROUP_CONCAT(
            CONCAT(a.description,' ',a.x,',',a.y) ORDER BY a.id
            SEPARATOR '$'
            ) AS answers
        FROM questions q
          JOIN answers a ON a.question_id = q.id
        GROUP BY q.description
      ) t;
    

    Will return

    +-------------+----------+----------+----------+----------+
    | description | answer_1 | answer_2 | answer_3 | answer_4 |
    +-------------+----------+----------+----------+----------+
    | Q1          | A1 1,2   | A2 3,4   |          |          |
    | Q2          | A3 5,6   | A4 7,8   | A5 9,10  |          |
    +-------------+----------+----------+----------+----------+
    2 rows in set (0.00 sec)
    

    I added an answer to the second question for the illustration.

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

Sidebar

Related Questions

Let's say I have two tables that look like this: TH TH TH TH
I have a 3 tables that look like this: (source: InsomniacGeek.com ) On the
I have a database that has two tables, these tables look like this codes
I have two tables that look like this: Supplier SN SNAME Stat City +----------+-------+----+--------+
I have a two tables that look like this (this is an example of
I have a table posts that could look like this: id | title |
On my pages I have a table with rows that typically look like this:
I have a number of mappers that look like this: mapper(Photo,photo_table, properties = {
I have two tables in my database that look like that: Customer: C_ID city
I have multiple buttons that gets created in a table which all look like

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.