0

I want to insert a row of data into table A which contains the columns: School, Name, Colour, Age. Using the where clause I will need to get this information from the following tables: School_table, Name_table, Colour_table and Age_table.

INSERT INTO A (School, Name, Colour, Age)
// how do I add the values from the other tables into their respective columns?
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
user22
  • 3
  • 4
  • 1
    If you can write a SELECT statement to fetch the data, then use `INSERT... SELECT` - https://stackoverflow.com/questions/5391344/insert-with-select – Nigel Ren Apr 24 '20 at 05:48
  • That is a standard thing you will have to learn from SQL, there are probably many tutorials out there on how to use SELECT. – Nigel Ren Apr 24 '20 at 05:50

1 Answers1

1

Use INSERT... SELECT. You can find the docs here

A small sample to get you started:

INSERT INTO A (School, Name, Colour, Age) SELECT value_from_school_table, value_from_name_table, ... FROM school_table JOIN name_table ON ... WHERE field = value

First start with writing a query that fetches all the values you want to insert. Then just append that select statement to your insert.

Markus Müller
  • 2,611
  • 1
  • 17
  • 25