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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:45:05+00:00 2026-06-07T22:45:05+00:00

How can I join multiple rows in just one single row through mysql? Example

  • 0

How can I join multiple rows in just one single row through mysql?

Example :

Student Table

Sno.| Name  |  Subjects
1.  | ABC   |  English
2.  | ABC   |  Mathematics
3.  | ABC   |  Science
4.  | FMC |  French
5.  | ABC   |  Russian
6.  | JBC   |  French

Now I want it in this format

Sno.| Name |   Sub1 |  Sub2 | Sub3 |  Sub4 |
1.  | ABC |   Eng  |  Maths| Science| Russian
2.  | FMC |    French| Null| Null   | Null
3.  | JBC |   French| Null | Null   | Null

I am not sure how to actually do it? And shall I create a view or a table?

I guess a view will be fine.

  • 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-07T22:45:07+00:00Added an answer on June 7, 2026 at 10:45 pm

    I agree with the other answers, that GROUP_CONCAT along with PHP to split the comma separated values is probably the best approach, however if for any other reason you needed the output you suggested via Pure SQL I would suggest one of the following appoaches.

    1. Self Joins

    SELECT  t1.Name, 
            MIN(t1.Subject) AS Sub1,
            MIN(t2.Subject) AS Sub2,
            MIN(t3.Subject) AS Sub3,
            MIN(t4.Subject) AS Sub4
    FROM    Students t1
            LEFT JOIN Students T2 
                ON t1.Name = t2.Name 
                AND t2.Subject > t1.Subject
            LEFT JOIN Students T3 
                ON t2.Name = t3.Name 
                AND t3.Subject > t2.Subject
            LEFT JOIN Students T4 
                ON t3.Name = t4.Name 
                AND t4.Subject > t3.Subject
    GROUP BY t1.Name;
    

    2. Using a ROW_NUMBER Type function to aggregate

    SELECT   Name,
             MAX(IF(RowNum = 1,Subject, NULL)) AS Sub1,
             MAX(IF(RowNum = 2,Subject, NULL)) AS Sub2,
             MAX(IF(RowNum = 3,Subject, NULL)) AS Sub3,
             MAX(IF(RowNum = 4,Subject, NULL)) AS Sub4
    FROM     (    SELECT   Name,
                           Subject,
                           @r:= IF(@Name = Name, @r + 1, 1) AS RowNum,
                           @Name:= Name AS Name2
                  FROM    Students,
                          (SELECT @Name:='') n,
                          (SELECT @r:= 0) r
                  ORDER BY Name, Sno
              ) t
    GROUP BY Name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How I can join this queries to single select (without temp table)? SELECT t2.value,
Ok so I know we can join tables 'across' in MySQL to tables using
In Vim, one can join two lines by typing capital J. However, these are
I have a join query which returns multiple rows ID SearchID Bool1 Bool2 Bool2
I'm trying to select multiple rows over different tables but I can't get it
In the Model class I use return $query->row(); to return single rows and return
I have one table called gallery . For each row in gallery there are
I had a query that returned multiple rows from a table. Then I converted
Does anyone know a simple way of copying multiple table rows and it's related
This may be asked in multiple forums but I just can't get it working.

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.