0

This is a follow up question to my previous question I would like to insert data into table with three of the columns being populated by a select-join queries. This is my sql statement::

INSERT INTO books ( gid, type, membernumber,  changedate, order, user, task)
  VALUES( '2b4493e8-cae8-4624-8177-fcc96553c5be',
          2,
          (SELECT member FROM mem m JOIN group g ON m.uid = g.sgroup),
          TIMESTAMP '2017-01-03 00:00:00.000',
          (SELECT order FROM orders o JOIN items i ON o.id = i.id),
          'mark',
          (SELECT task FROM tasks t JOIN lists l ON t.id = l.id),
        );

What i want to achieve is for each insert, the membernumber, order and task will be taken from the mem, order and tasks tables base on the JOIN it has with their respective join tables.

6
  • tag the specific database which you are using Commented Jun 5, 2018 at 8:52
  • How are the tables member, orders and task related? If you can build a single query that returns the desired rows from those tables, you can easily use that as the source of an insert statement (instead of the values clause) Commented Jun 5, 2018 at 9:08
  • @a_horse This is just an example. The relationship is not important. I just want the correct syntax. Commented Jun 5, 2018 at 9:10
  • The relationship is important. Only if you are able to create a single query that returns the information you want to insert, you can do this properly. As shown your query makes no sense whatsoever. You are inserting a single row, but retrieving multiple rows from those "related" tables and you are trying to put the columns values for many rows into one column of a single row. Commented Jun 5, 2018 at 9:12
  • @a_horse_with_no_name like i said the above code is just an example. in the real code there is relationship. Assume there is a relationship in the above code, what will the correct syntax be? I am looking for the correct syntax Commented Jun 5, 2018 at 9:14

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.