Home > Mobile >  How can i insert into table result from another sql statement that returns 2 rows
How can i insert into table result from another sql statement that returns 2 rows

Time:07-19

I have two tables message and user. I would like to insert into the message table result from the users table which looks like this

select id from "user" ORDER BY random() limit 2;

And now insert this result instead of:

'uuid-from users table',
'uuid-from users table'

Insert message query

insert into message (id, to_user_id, from_user_id, content)
select uuid_generate_v4(),
'uuid-from users table',
'uuid-from users table',
'Some content';

How can i achieve this?

CodePudding user response:

Just

insert into message 
select uuid_generate_v4(), to_user_id, from_user_id, 'Some content' from users; 
  • Related