I need to insert a row into a table by selecting value from multiple tables. I want a row to be inserted from multiple tables.
insert into tab(id,name,date) values
((select max(id)+1 from tab),(select name from tab1),now());
Can you tell me how to do achieve this?
Whether this can be achieved by simple query itself or stored procedure is the only option?
Best Answer
This is typically achieved by writing a
SELECT
statement that outputs the fields in the same order as theINSERT
statement and using that instead of aVALUES
clause. For example, one might:The point is that you can just do an
to insert rows. You do not have to create a
VALUES
clause and mess with subqueries.