Luke CheerfulPlum Pace
Luke CheerfulPlum Pace

Reputation: 293

MySQL Inserting a record for each already existing record

Hi guys basically I have a query that select all the clientId where the flagId = 6

which is:

SELECT clientId from ClientsFlags WHERE flagId = 6;

Now I want to take the results from this query and create a new record for the each of the results obtained in the select statement

INSERT INTO clientFlags clientId, flagId VALUES('The client ID's that were obtained in the select query', 42);

What is the best way to combine these statements?

Upvotes: 1

Views: 88

Answers (5)

Md. Maruf Hossain
Md. Maruf Hossain

Reputation: 922

You Can Do As Like Following

 INSERT INTO `clientFlags` 
 SELECT * FROM `clientFlags` WHERE `flagId` = 6

Note: Also you will have to be remember that any field could not be unique or primary key ...

Upvotes: 0

Arun
Arun

Reputation: 125

INSERT INTO clientFlags (clientId, flagId) SELECT clientId, 42 from ClientsFlags WHERE flagId = 6;

Upvotes: 0

Miguel Matos
Miguel Matos

Reputation: 191

You can use the INSERT ... SELECT syntax

INSERT INTO clientFlags (clientId, flagId) 
    SELECT clientId, 42 
    FROM ClientsFlags 
    WHERE flagId = 6;

Something like this, I haven't test it for syntax errors

Upvotes: 2

Muthu Kumaran
Muthu Kumaran

Reputation: 17910

Use INSERT ... SELECT Syntax

Your query should look like,

INSERT INTO clientFlags (clientId, flagId) 
   SELECT clientId, flagId 
   FROM ClientsFlags WHERE flagId = 6;

Upvotes: 0

Hanky Panky
Hanky Panky

Reputation: 46900

INSERT INTO clientFlags (SELECT clientId,42 from ClientsFlags WHERE flagId = 6)

Upvotes: 0

Related Questions