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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:16:32+00:00 2026-06-11T23:16:32+00:00

How do i return multiple columns from a table ? My Course table: |

  • 0

How do i return multiple columns from a table ?

My Course table:

| Course | PersId | Taskid | Status |

Computer < > User1 <> 1 < > confirmed
Computer < >User2 <> 1 <> unconfirmed

Computer < >User3 <> 1 <> unconfirmed

Computer < >User1 <> 2 <> confirmed

Computer < >User2 <> 2 <> confirmed

Computer < >User3 <> 2 <> unconfirmed

I want it to return like this:

| PersId | Task_1 | Task_2 |

User1 <> confirmed < > confirmed

User2 <> unconfirmed <> confirmed

User3 <> unconfirmed <> unconfirmed

Question 2:
There are other courses(math, english etc) in my table with more tasks than two. Do i need to use some kind of iteration to return the task columns ? because i dont want to make a SQL query for each single course(over 100).

thanks in advance

  • 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-11T23:16:33+00:00Added an answer on June 11, 2026 at 11:16 pm

    You can use a CASE and an aggregate:

    select persid,
      max(case when taskid = 1 then status end) as Task1,
      max(case when taskid = 2 then status end) as Task2
    from course
    group by persid
    

    If you want to include the course info:

    select persid,
      course,
      max(case when taskid = '1' then status end) as Task1,
      max(case when taskid = '2' then status end) as Task2
    from course
    group by persid, course
    order by course, persid
    

    See SQL Fiddle with Demo

    If you have an unknown number of tasks, then you can use a prepared statement to generate this dynamically:

    SET @sql = NULL;
    SELECT
      GROUP_CONCAT(DISTINCT
        CONCAT(
          'max(case when taskid = ''',
          taskid,
          ''' then status end) AS Task_',
          taskid
        )
      ) INTO @sql
    FROM course;
    
    SET @sql = CONCAT('SELECT persid, course, ', @sql, ' 
                      FROM course 
                      group by persid, course
                      order by course, persid');
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

    See SQL Fiddle with Demo

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

Sidebar

Related Questions

I'm selecting multiple columns from a table (id, name). I would like to get
I have need to return multiple results from a subquery and have been unable
I need to return multiple values from a ColdFusion function in an ajax callback
Do Java collections have a built-in method to return multiple items from that collection?
List of values, with multiple columns and multiple return values in Apex. It's a
I have a very large SELECT stored procedure that has multiple return columns and
I have a table of orders with multiple date columns (Department finished dates). I'd
I want to select columns from a table where the values of columns are
I'm currently trying to return multiple JSON objects in one of my controllers as
Important : Please see this very much related question: Return multiple values in C++

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.