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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:29:23+00:00 2026-06-13T13:29:23+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   |  1
2.  | ABC   |  3
3.  | ABC   |  4
4.  | FMC   |  2
5.  | ABC   |  4
6.  | JBC   |  4

Papers Table:

Sno. | Paper Name | Type 
 1.      French     Optional
 2.      English    Mandatory
 3.      Japenese   Optional
 4.      Maths      Optional

Now I want it in this format

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

What i want to select is papers name from papers table and student name, four subjects from the other table. I only want to see those paper which are optional. I am not sure what to do?

  • 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-13T13:29:24+00:00Added an answer on June 13, 2026 at 1:29 pm

    I think the concept you are looking for is pivoting.

    Version with join

    select 
      name,
      max(if(s.subject = 1 and p.type = 'Optional', p.paper_name, null)) as subject1,
      max(if(s.subject = 2 and p.type = 'Optional', p.paper_name, null)) as subject2,
      max(if(s.subject = 3 and p.type = 'Optional', p.paper_name, null)) as subject3,
      max(if(s.subject = 4 and p.type = 'Optional', p.paper_name, null)) as subject4
    from 
      students s
    inner join
      papers p on p.sno = s.subject
    group by s.name
    

    SQL Fiddle Demo

    Version with subselects instead of a join

    select 
      name,
      max(
        case 
          when subject = 1 
            then (select paper_name from papers p where p.sno = subject and type = 'Optional') 
          else 
            null 
        end) as subject1, 
      max(
        case 
          when subject = 2 
            then (select paper_name from papers p where p.sno = subject and type = 'Optional') 
          else 
            null 
        end) as subject2,
      max(
        case 
          when subject = 3 
            then (select paper_name from papers p where p.sno = subject and type = 'Optional') 
          else 
            null 
        end) as subject3,
      max(
        case 
          when subject = 4
            then (select paper_name from papers p where p.sno = subject and type = 'Optional') 
          else 
            null 
        end) as subject4
    from 
      students
    group by 
      name
    

    SQL Fiddle Demo

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

Sidebar

Related Questions

How can I join multiple rows in just one single row through mysql? Example
How I can join this queries to single select (without temp table)? SELECT t2.value,
Q: How can i make mysql query search in multiple rows What i want
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 had a query that returned multiple rows from a table. Then I converted
In the Model class I use return $query->row(); to return single rows and return
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
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.