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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:43:16+00:00 2026-06-15T21:43:16+00:00

I have a relational database with one main table linked to other tables with

  • 0

I have a relational database with one main table linked to other tables with foreign keys. I am trying to write a reporting tool in PHP to grab all the data out but have gotten stck with one part of it.

here is my query so far:

SELECT * 
FROM student s 
LEFT OUTER JOIN ep e ON s.sID = e.sID 
LEFT OUTER JOIN ntc n ON s.sID = n.sID 
LEFT OUTER JOIN pk p ON s.sID = p.sID 
LEFT OUTER JOIN roa r ON s.sID = r.sID 
WHERE s.sID = '$id'

ep, ntc and pk are all 1:1 with student so there are no problems. But the roa table holds multiple records (spanning multiple rows) for each student.

roa table structure:

+----------+--------------+------+-----+-------------------+----------------+
| Field    | Type         | Null | Key | Default           | Extra          |
+----------+--------------+------+-----+-------------------+----------------+
| roaID    | int(11)      | NO   | PRI | NULL              | auto_increment |
| sID      | int(11)      | NO   | MUL | NULL              |                |
| roa      | varchar(255) | NO   |     | NULL              |                |
| roaStaff | varchar(50)  | NO   |     | NULL              |                |
| visible  | tinyint(1)   | NO   |     | 1                 |                |
+----------+--------------+------+-----+-------------------+----------------+

where a select * from roa where sID = 1 returns:

+-------+-----+---------------+----------+---------+
| roaID | sID | roa           | roaStaff | visible |
+-------+-----+---------------+----------+---------+
|    41 | 1   | Description 1 | Staff 1  |       1 |
|    60 | 1   | Description 2 | Staff 2  |       1 |
+-------+-----+---------------+----------+---------+

what I am after my original query to achieve is a result something like:

<-student, etc data
..-------+-----+-----------------------------------------------+---------+
.. roaID | sID | roa                                           | visible |
..-------+-----+-----------------------------------------------+---------+
..    41 | 1   | Description 1 Staff 1, Description 2 Staff 2  |       1 |
...
...

so that both the roa.roa and the roa.roaStaff are all in one cell.

I have tried using GROUP_CONCAT but have only been able to get it to work for one column, not group multiple columns together.

any help is greatly appreciated.

regards.

  • 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-15T21:43:17+00:00Added an answer on June 15, 2026 at 9:43 pm

    try something like this,

    SELECT ......, GROUP_CONCAT(CONCAT(roa, ' ', roaStaff)) roa
    FROM ...
    

    so when you plug it in your query,

    SELECT * 
    FROM student s 
    LEFT OUTER JOIN ep e ON s.sID = e.sID 
    LEFT OUTER JOIN ntc n ON s.sID = n.sID 
    LEFT OUTER JOIN pk p ON s.sID = p.sID 
    LEFT OUTER JOIN 
    (
        SELECT sID, GROUP_CONCAT(CONCAT(roa, ' ', roaStaff)) roa
        FROM roa 
        GROUP BY sID
    ) r ON s.sID = r.sID 
    WHERE s.sID = '$id'
    

    follow-up question, how about the visible field on table roa? How would you want it to be shown

    SIDENOTE

    your query is vulnerable with SQL INJECTION, please read the article below to learn how to protect from it

    • How can I prevent SQL injection in PHP?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a relational table with two foreign-keys pointing to same table. The first
I have a relational database with about 100 tables. Each table has unique, numerical,
Let's say we have three tables in a relational database: Person {id, name} Obstacle
I have some relational MySQL database with dozens of tables... My query is 1000
I know, relational database is a database where fields in one table are linking
I have a relational database with a Client table, containing id , name ,
I have a relational database with about 15 tables that will be used in
Let's say I have a relational database with tables: OrderableCategories and Orderables. They are
I have few tables in the database with parent table say P is having
I have a question about Criteria method, one-to-many relation to the database, 'one' is

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.