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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:41:35+00:00 2026-05-28T01:41:35+00:00

If I have a table: CREATE TABLE Kids ( kid_id int(10) UNSIGNED NOT NULL

  • 0

If I have a table:

CREATE TABLE Kids (
    kid_id int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    kid_name varchar(45) NOT NULL,
    kid_favcolors (text) NULL,
    PRIMARY_KEY(kid_id)
    )

and I have a table:

CREATE TABLE Colors (
    color_id int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    color_name varchar(45) NOT NULL,
    PRIMARY_KEY(color_id)
)

With rows that reference favorite colors via comma separated ids:

INSERT INTO Kids(kid_name, kid_favcolors) VALUES('Joe','1,2,3,4,5');
INSERT INTO Kids(kid_name, kid_favcolors) VALUES('Mary','1,2,3');

How would I create a statement that would return each entry in the Kid database with the Kid_name and the color_name of all of the ids referenced in the kid_favcolors column.

For example:

COLORS:
color_id    color_name
1           yellow
2           green
3           blue
4           purple
5           red
6           brown
7           black
KIDS:
kid_id      kid_name    kid_favcolors
1           Joe         1,2,3,4,5
2           Mary        1,2,3

and I want to retrieve

kid_id      kid_name    favorite_colors
1           Joe         yellow, green, blue, purple, red
2           Mary        yellow, green, blue

The queries are being executed on existing data and database structure. If there is no feasible solution, rearranging the structure of the data is possible but I assume would add a considerable amount of time to the solution. Either way, would like some help.

I am able to retrieve the colors from the database using:

SELECT STUFF((SELECT ', ' + color_name FROM colors WHERE color_id IN (1,2,3,4,5) FOR XML PATH('')),1, 2, '') AS colors

        colors
1       yellow, green, blue, purple, red

But when I try a more complex query I can’t seem to find a way to incorporate the above statement by pulling the ids from the Kids table.

SELECT kids.kid_id, kids.kid_name, favorite_colors FROM kids JOIN colors ON colors.id IN kids.kid_favcolors as favorite_colors

While it looks like it could work to me, not working. Not sure if I’m way off or really close.

  • 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-28T01:41:36+00:00Added an answer on May 28, 2026 at 1:41 am

    I have worked up a solution; though it’s not pretty, it works.

    SELECT 
      kid_id,
      kid_name, 
      (SELECT STUFF(
         (SELECT ', '+color_name FROM colors WHERE color_id in 
            (SELECT s from SplitString(kid_favcolors,',')) 
         FOR XML PATH(''))
       ,1,2,'')) AS favorite_colors 
    FROM KIDS
    
    kid_id      kid_name    favorite_colors
    1           Joe         yellow, green, blue, purple, red
    2           Mary        yellow, green, blue
    

    I have talked to them about reworking the DB and implementing a current administration site to accommodate the changes

    Split String function I used:

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    create function [dbo].[SplitString] 
    (
        @str nvarchar(4000), 
        @separator char(1)
    )
    returns table
    AS
    return (
        with tokens(p, a, b) AS (
            select 
                1, 
                1, 
                charindex(@separator, @str)
            union all
            select
                p + 1, 
                b + 1, 
                charindex(@separator, @str, b + 1)
            from tokens
            where b > 0
        )
        select
            p-1 zeroBasedOccurance,
            substring(
                @str, 
                a, 
                case when b > 0 then b-a ELSE 4000 end) 
            AS s
        from tokens
      )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this table CREATE TABLE `codes` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
I have this table: CREATE TABLE `test` ( `ID` int(11) NOT NULL auto_increment, `text`
I have this table: CREATE TABLE `point` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `siteid`
I have this table CREATE TABLE `zipcodes2` ( `ID` int(11) NOT NULL auto_increment, `zipcode`
I have this table: CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255)
I have a table: CREATE TABLE `data_table` ( `data_id` INT NOT NULL AUTO_INCREMENT PRIMARY
I have this table: CREATE TABLE `search_engine_rankings` ( `id` int(11) NOT NULL AUTO_INCREMENT, `keyword_id`
I have a table papers CREATE TABLE `papers` ( `id` int(11) NOT NULL AUTO_INCREMENT,
I have this table: create table user_roles ( user_id INT UNSIGNED NOT NULL, role_id
I have this table CREATE TABLE [dbo].[friend_blocked_list]( [subdomain] [varchar](50) NOT NULL, [un] [nvarchar](50) NOT

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.