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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:14:46+00:00 2026-06-17T16:14:46+00:00

I want to do something like this Insert into abc(col1, col2, col3) select col1,

  • 0

I want to do something like this

Insert into abc(col1, col2, col3) select col1, 1, col3 from xyz where somecondition
Insert into abc(col1, col2, col3) select col1, 2, col3 from xyz where somecondition
Insert into abc(col1, col2, col3) select col1, 3, col3 from xyz where somecondition
Insert into abc(col1, col2, col3) select col1, 4, col3 from xyz where somecondition
...

So there is only second column value that is changing and all else are same.

Is there any better or compact way to do the above?
May be something in one query!

I am using MySql

Edit:

I am using this inside a stored procedure.

  • 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-17T16:14:47+00:00Added an answer on June 17, 2026 at 4:14 pm

    If somecondition is the same and you need sequential numbers for col2 you can do following:

    INSERT INTO abc(col1, col2, col3)
    SELECT col1, @n := @n + 1, col3
    FROM xyz, (SELECT @n:= 0) n
    WHERE somecondition
    

    UPDATE

    If your WHERE conditions applied to xyz ensures that only one row will be returned and your xyz table has more than 9 records (although you can CROSS with any other table) you can leverage CROSS JOIN AND LIMIT to replicate your row N (9 in the example) times with single INSERT-SELECT statement like this:

    INSERT INTO abc(col1, col2, col3)
    SELECT t1.col1, @n := @n+1, t1.col3 
      FROM xyz AS t1 CROSS JOIN xyz AS t2,(SELECT @n:= 0) n 
    WHERE t1.col2=2 LIMIT 9
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to do something like this INSERT INTO link_list(link, status, hash_type, hash_id) VALUES(@link,
Is there any way to do something like this in django? INSERT INTO t
I want to do something like this: INSERT INTO TABLEA ( COLUMN1, COLUMN2, COLUMN
I want to do something like this INSERT INTO t (t.a, t.b, t.c) VALUES
I want to do something like this in SQL Server: SELECT pt.quantity AS 'QTY'
I have to do something like this insert into object (name, value, first_node) values
I have a file with contents something like this: INSERT INTO table VALUES (NULL,'°
Currently I'm trying this: $sql_words = array(select,update,set, from, where, and, order by,insert into,delete); echo
I want something like this: abcdab.search(/a/g) //return [0,4] Is it possible?
I want to parse something like this: Hi [{tagname:content}] [{tag1:xnkudfdhkfujhkdjki diidfo now nested tag

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.