Reputation: 293
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
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
Reputation: 125
INSERT INTO clientFlags (clientId, flagId) SELECT clientId, 42 from ClientsFlags WHERE flagId = 6;
Upvotes: 0
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
Reputation: 17910
Your query should look like,
INSERT INTO clientFlags (clientId, flagId)
SELECT clientId, flagId
FROM ClientsFlags WHERE flagId = 6;
Upvotes: 0
Reputation: 46900
INSERT INTO clientFlags (SELECT clientId,42 from ClientsFlags WHERE flagId = 6)
Upvotes: 0