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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:04:44+00:00 2026-06-16T01:04:44+00:00

How can i replace one row with 5 rows with same data in mysql.for

  • 0

How can i replace one row with 5 rows with same data in mysql.for example i have this table

 id   |   companyId   |      name       |  open  |  close
 1    |    1          |    mon-fri      |  10:00 |  19:00  
 2    |    1          |      sat        |  10:00 |  12:00 
 3    |    1          |      sun        |  10:00 |  12:00 
 4    |    2          |    mon-fri      |  10:00 |  16:00  
 5    |    2          |      sat        |  10:00 |  13:00 
 6    |    2          |      sun        |  10:00 |  13:00 

I want to convert name field where its mon-fri to mon , tues , wed , thur , fri .

 id   |   companyId   |      name       |  open  |  close
 1    |    1          |    mon     |  10:00 |  19:00  
 2    |    1          |    tues    |  10:00 |  19:00 
 3    |    1          |    wed     |  10:00 |  19:00 
 4    |    1          |    thur    |  10:00 |  19:00   
 5    |    1          |    fri     |  10:00 |  19:00  
 6    |    1          |    sat     |  10:00 |  12:00 
 7    |    1          |    sun     |  10:00 |  12:00 
.
.
.
  • 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-16T01:04:45+00:00Added an answer on June 16, 2026 at 1:04 am

    You can’t do this via an UPDATE, but what you can do is a series of INSERT INTO ... SELECT making use of some string literals, followed by a DELETE to remove the rows you expanded out.

    /* First insert a row for each mon,tue,wed,thur,fri */
    /* Column values are copied from the existing mon-fri row, and use the literal strings 'mon', 'tue', etc as the new `name` */
    INSERT INTO yourtable (`companyId`, `name`, `open`, `close`)
      SELECT `companyId`, 'mon', `open`, `close` FROM yourtable WHERE `name` = 'mon-fri'
    INSERT INTO yourtable (`companyId`, `name`, `open`, `close`)
      SELECT `companyId`, 'tue', `open`, `close` FROM yourtable WHERE `name` = 'mon-fri'
    INSERT INTO yourtable (`companyId`, `name`, `open`, `close`)
      SELECT `companyId`, 'wed', `open`, `close` FROM yourtable WHERE `name` = 'mon-fri'
    INSERT INTO yourtable (`companyId`, `name`, `open`, `close`)
      SELECT `companyId`, 'thur', `open`, `close` FROM yourtable WHERE `name` = 'mon-fri'
    INSERT INTO yourtable (`companyId`, `name`, `open`, `close`)
      SELECT `companyId`, 'fri', `open`, `close` FROM yourtable WHERE `name` = 'mon-fri'
    
    /* Then delete the original mon-fri rows which you just expanded out */
    DELETE FROM yourtable WHERE `name` = 'mon-fri'
    

    http://sqlfiddle.com/#!2/cae33/1

    (Credit where due: this excellent suggestion was the work of Andriy M in an unsolicited edit)

    You could also reduce the number of statements, as well as the number of table scans, by using a virtual table, like this:

    /* Cross-join the existing 'mon-fri' rows with a virtual table of day names
       of 'mon' through 'fri' and insert the resulting set back into your table */
    INSERT INTO yourtable (`companyId`, `name`, `open`, `close`)
      SELECT t.`companyId`, v.`name`, t.`open`, t.`close`
      FROM yourtable t
      CROSS JOIN (
        SELECT 'mon' AS `name` UNION ALL
        SELECT 'tue' UNION ALL
        SELECT 'wed' UNION ALL
        SELECT 'thur' UNION ALL
        SELECT 'fri'
      ) v
      WHERE t.`name` = 'mon-fri';
    
    /* Then delete the original mon-fri rows which you just expanded out */
    DELETE FROM yourtable WHERE `name` = 'mon-fri';
    

    http://sqlfiddle.com/#!2/98e35/1

    Update after comments:

    There is no real value to forcing their order when you insert the rows, that is best done in SELECT.

    To order by day for each companyId, it will require some work no matter which way it is done. You could call LOWER(STR_TO_DATE()) with %a and compare the weekday value, however you would also need to change your thur to thu since that is how MySQL abbreviates it. That results in a bunch of function calls.

    Instead, you can use a CASE.... in the ORDER BY to assign ordinal values to each day, like:

    ORDER BY
     companyId,
     CASE `name`
       WHEN 'sun' THEN 1 
       WHEN 'mon' THEN 2
       WHEN 'tue' THEN 3
       WHEN 'wed' THEN 4
       WHEN 'thur' THEN 5
       WHEN 'fri' THEN 6
       WHEN 'sat' THEN 7
     ELSE 8 END
    

    Neither of the above methods is going to be friendly to indexing though.

    If you need this to be more performant, I would recommend against storing the strings 'mon','tue','wed',etc in the first place, and instead storing their associated weekday value. See DAYOFWEEK() for details.

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

Sidebar

Related Questions

I have a 300.000 rows table; one of the columns is a varchar() but
I have a table similar to this, but with many rows: <table width=400px id=mytable>
I have a databases table with ~50K rows in it, each row represents a
I have only one column roll number in my oracle table. Lots of data
How can one replace a part of a line with sed? The line DBSERVERNAME
Can any one tell me how can i replace the slideup function with fadeout('slow')
Can anyone please explain how to replace the values of one column with the
so 1GvG:s/..../g can replace over an entire buffer However, suppose I have multiple vim
I have a CSV file with many rows in which I need to update/replace
I'm writing a webpage that has a table with rows that slide open and

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.