INSERT SELECT statement

Hi there,

I having a problem with a MySQL INSERT … SELECT statement:


INSERT INTO
`products`
(
  `id`,
  `image`,
  `title`,
  `description`,
  `price`,
  `VAT`,
  `related_products`
)
SELECT
  'CR25',
  `id` FROM `images` WHERE `images`.`title` = 'Some Image Title',
  'Product Title',
  'Product Description',
  4.99,
  0,
  'CR26'


Throws an error:
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 ’ ‘Product Title’

I’ve also tried


INSERT INTO
`products`
(
  `id`,
  `image`,
  `title`,
  `description`,
  `price`,
  `VAT`,
  `related_products`
)
VALUES
(
  'CR25',
  (`id` FROM `images` WHERE `images`.`title` = 'Some Image Title'),
  'Product Title',
  'Product Description',
  4.99,
  0,
  'CR26'
)


But I get a similar error message:
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 ’ ’

As far as I can tell I’m using the correct syntax.

Any ideas?

Cheers
M

almost, but not quite :slight_smile:

all the constant values (strings, numbers) should be included in the SELECT clause…

INSERT 
  INTO products
     ( id
     , image
     , title
     , description
     , price
     , VAT
     , related_products ) 
SELECT 'CR25'
     , id 
     , 'Product Title'
     , 'Product Description'
     , 4.99
     , 0
     , 'CR26'
  FROM images 
 WHERE title = 'Some Image Title'

As always r937, worked like a charm! Many thanks!

M