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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:34:06+00:00 2026-05-31T15:34:06+00:00

I have following legacy database setup: CREATE TABLE `categories` ( `id` int(11) NOT NULL

  • 0

I have following legacy database setup:

CREATE TABLE `categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
)

CREATE TABLE `items` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `category_ids` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
)

in which category_ids is a string of category id’s separated by comas: 1, 10, 15, 6. Is there a way to convert this database to more conventional one (using three tables, one for storing relationships), using only SQL and no other scripts?

  • 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-31T15:34:07+00:00Added an answer on May 31, 2026 at 3:34 pm

    MySQL doesn’t have CROSS APPLY or recursive CTEs, which would be the simplest routes.

    But you’re only dong this once, so you only need a quick hack.

    First, find out the maximum number of items in the category list…

    SELECT
      MAX(LEN(category_ids) - LEN(REPLACE(category_ids, ',', '')) + 1) AS max_items
    FROM
      items
    

    Then you can do something like this…

    SELECT
      items.id,
      SUBSTRING_INDEX(
        SUBSTRING_INDEX(
          items.category_ids,
          ',',
          map.id  -- Get the first 'n' items from the list
        ),
        ',',
        -1        -- Get the last item from (the first 'n' items from the list)
      ) AS category_id
    FROM
      items
    INNER JOIN
    (
                SELECT 1 as id
      UNION ALL SELECT 2 as id
      UNION ALL SELECT 3 as id
      etc, etc, up to the max number of items found previously
    )
      AS map
        ON LEN(items.category_ids) - LEN(REPLACE(items.category_ids, ',', '')) + 1 >= map.id
    

    I haven’t tested it, but I’m assuming that SUBSTRING_INDEX('last', ',', -1) returns 'last'.

    I’m no MySQL expert, so this may not be optimal, but as a one time quick win thistype of structure should work…

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

Sidebar

Related Questions

I have a legacy database with the following table (note: no primary key) It
I have the following bit of legacy C++ code that does not compile: #include
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
Im working on a legacy database, that cant be changed. I have a table
We have a legacy database that uses strings as primary keys. I want to
I am using a legacy database and I have a table called TEAM_PLAYER. I
I have the following scenario: Due to an unfortunate legacy database, I have some
I have the following tables in a legacy database: Table1 + Table1ID PK +
In a legacy MySQL database I have a table with a field that is
I have a question similar to @ManyToMany without join table (legacy database) with an

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.