I got my answer from Bill. But how do I insert other default values not found on the loop select?
I'm doing this which works:
SET @createdAt = STR_TO_DATE(NOW(), '%Y-%m-%d %H:%i:%s');
INSERT IGNORE INTO fancy_table (userId, content, createdAt)
-- Values
SELECT
userId,
content,
-- I faked this
REPLACE(some_random_col, 'sometimes-value-does-not-match', @createdAt) as createdAt
FROM users
WHERE foo = 'bar';
What if I want to update more columns of fancy_table
, how to do that? The "fake" REPLACE
column is wrong to me.
EDIT:
How can I achieve something like this:
SET @createdAt = STR_TO_DATE(NOW(), '%Y-%m-%d %H:%i:%s');
INSERT IGNORE INTO fancy_table (userId, content, createdAt, etc...)
VALUES (
(SELECT
userId,
content
FROM users
WHERE foo = 'bar';),
@createdAt,
etc...
)