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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:05:24+00:00 2026-05-30T04:05:24+00:00

Is it possible to use a subselect as one of the returned columns of

  • 0

Is it possible to use a subselect as one of the returned columns of a query?

For example, I am using MySql, and I have a table containing a list of IDs associated with some data. I would like to convert the IDs associated with a given item of data from another table into a comma delimited VarChar list. I know I can do that using something along the lines of:

DECLARE pr_StudentId INT;   -- An input parameter

DECLARE @Classes VARCHAR(4096);

SELECT @Classes = COALESCE(@Classes + ',', '') + ClassTable.ClassId
FROM ClassTable, StudentTable
WHERE ClassTable.Id = StudentTable.Id
AND StudentTable.Id = p_StudentId;

However, the problem is that I need to return that within another SELECT along with other data from (for example) the StudentTable, which is in a stored procedure. So far I have this, but I’m not quite sure how to get it working:

SELECT
    StudentTable.Id,
    StudentTable.Name,
    (
        SELECT @Classes
        FROM
        (
            SELECT @Classes = COALESCE(@Classes + ',','') + ClassTable.ClassId
            FROM ClassTable, StudentTable
            WHERE ClassTable.Id = StudentTable.Id
        )
    ) AS ClassList,
    ...
FROM StudentTable ....
WHERE ....

Can anyone shed some light on whether this is the correct way to do this, if there is a better way, or if it is even possible?

Many 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-05-30T04:05:26+00:00Added an answer on May 30, 2026 at 4:05 am

    It’s even easier in MySQL. Check out the GROUP_CONCAT function.

    SELECT st.Id, st.Name, GROUP_CONCAT(ct.ClassId) AS ClassList
        FROM StudentTable st
            INNER JOIN ClassTable ct
                ON st.Id = ct.Id
        GROUP BY st.Id, st.Name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible use a MySQL query to perform this kind of check? If
It is possible use Sphinx RT index in conjunction with Mysql innodb table for
Is it possible use RESTKit with ASIHTTPRequest? I have been using ASIHTTPRequest but it
I'm having an SQL query (MSSQLSERVER) where I add columns to the resultset using
I have a MySQL query that results in something like this: person | some_info
I have a materialized path-driven bulletin board. It is using the following query to
Is possible to use different version of Perl without using the SET or manipulating
I have recently started looking into Google Charts API for possible use within the
Can functors be defined locally within the body of a function? One possible use
I have reviewed DashCommerce, nopCommerce and DotShoppingCart for possible use and all of them

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.