I am trying to insert values into selected columns of table, but only insert the results of a MySQL statement. the select statement works correctly by itself and returns the results.
when combined with the insert statement it is fails with
error incorrect syntax near `dedspgoods`.`case number`.
Can anyone assist me with the correct syntax? my erronous syntax is as below:
insert into despgoods_alldetails
(`case number`, weight, customer, transporttypename)
values
( select despgoods.`case number`
, despgoods.weight
, despgoods.customer
, customers.transporttypename
from despgoods
inner join customers
on despgoods.customer = customers.customer )
case
is a reserved word. Need to put 'case number' in quotes.here it goes
there. that should work fine. Remember, do not name your variables with spaces, because it can get real tricky. I think you had an error in case number because it has a space in between, when case is a function in MySQL. Let me know if you need anything else.
As stated by @EdHeal, you'll need to enclose
case number
between quotes or backticks as there is a space in the column name (the fact thatCASE
is a reserved word is actually only an interesting detail, but that's not what breaks the query in this case).If this is the
SELECT
that works:Then try this (notice there is no
VALUES
clause if you want to insert the result set of aSELECT
: