3

I have a table in which I want to insert data, and the values itself have to come from multiple tables. This has to be done as follows, by reading the MySQL documentation:

insert into flight(airlinecompanyId,planetypeId)
select id from airlinecompany where naam = 'Brussels Airlines',
select id from planeType where type = 'Boeing 737';

So simply explained, I want to insert the id's from the tables airlinecompany and planetype that I demand via the where clausule, into the flight table column.

When I try this query, I keep getting the following error:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' select id from planeType where type = 'Boeing 737'' at line 2

Someone who has a solution?

4
  • 1
    How are these two tables (airlinecomapny, planetype) related? You are inserting two columns -- do the two tables have an exactly identical number of ids? Commented Apr 13, 2014 at 21:01
  • They don't have a relationship but airlineCompanyId and planetypeId are both foreign keys in the flight table, if that's what you mean? Commented Apr 13, 2014 at 21:06
  • What is your goal then? If planeType and airlinecompany are not related themselves, are you trying to insert a single row for those two values? Commented Apr 13, 2014 at 21:07
  • oi60.tinypic.com/nx46t4.jpg This basically is the ERD. I just want to have a seperate table with a planetype together with a airlinecompany... Commented Apr 13, 2014 at 21:12

1 Answer 1

5

Having no relationship between airlinecompany and planeType, you needn't necessarily perform a JOIN to do the insert, but since you are creating only a single row in flight, it can easily be done with subselects, wrapping each table's SELECT statement in ()

INSERT INTO flight (airlinecompanyId, planetypeId)
  SELECT
    (SELECT id FROM airlinecompany WHERE naam = 'Brussels Airlines'),
    (SELECT id FROM planeType WHERE type = 'Boeing 737')
  /* MySQL will permit this with no FROM clause */

It can alternatively be done with a CROSS JOIN, since there's only one possible row returned:

INSERT INTO flight (airlinecompanyId, planetypeId)
  SELECT
    ac.id,
    pt.id
  FROM
    airlinecompany ac
    CROSS JOIN planeType pt
  WHERE
    ac.naam = 'Brussels Airlines'
    AND pt.type = 'Boeing 737'
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, the above one was the most easy one and did the job! Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.