Copy multiple records to the same table with change of values

Asked

Viewed 44 times

0

I have the following query:

INSERT INTO tags group_tags, title, active
SELECT group_tags, title, active FROM tags WHERE group_tags = 2;

First

How to run this query for each table record that contains group_tags = 2?


According to

For each new record, how to set the value of group_tags for 1?

4 answers

2


INSERT INTO tags (group_tags, title, active)
SELECT 1 as 'group_tags', title, active FROM tags WHERE group_tags = 2;

Run only the second line and see if it is the expected result.

  • Thank you for the reply, it worked perfectly.

0

INSERT INTO tags (group_tags, title, active)
SELECT 1, title, active FROM tags WHERE group_tags = 2;

-1

hi,

see this way

INSERT INTO tags (1, title, active)
SELECT title, active FROM tags WHERE group_tags = 2;
  • Why the 1 entered as column name in INSERT?

  • All right, my ball food

-1

Experiment as follows:

INSERT INTO tags(group_tags, title, active)
    with cte as (
        SELECT group_tags, title, active FROM tags WHERE group_tags = 2
    )
    SELECT  1, title, active FROM cte

The first part

with cte as (
     SELECT group_tags, title, active FROM tags WHERE group_tags = 2
)

Creates a different context as another SQL query.

SELECT  1, title, active FROM cte 

Make necessary changes before inserting.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.