Y
Y
Yuriy Berg2016-10-19 13:17:29
MySQL
Yuriy Berg, 2016-10-19 13:17:29

SQL how to combine INSERT INTO and SELECT?

I have this SQL query.

SELECT url_img, object_id FROM images 
  INNER JOIN products ON images.object_id = products.id 
  WHERE images.object = "product"

From the images table, all fields are selected according to the given conditions.
But the task is to throw this result into the products table in the image_url field
INSERT INTO products (image_url)
SELECT url_img, object_id FROM images 
  INNER JOIN products ON images.object_id = products.id 
  WHERE images.object = "product"

UPDATE products
SET products.image_url = image.url_img
SELECT url_img, object_id FROM images 
  INNER JOIN products ON images.object_id = products.id 
  WHERE images.object = "product"

But nothing happens MySQL writes this:
Column count doesn't match value count at row 1

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2016-10-19
@art_haacki

UPDATE `products`
  LEFT JOIN `images` ON `images`.`object_id` = `products`.`id`
  SET `products`.`images` = `products`.`url`

M
mletov, 2016-10-19
@mletov

INSERT INTO products (image_url)
SELECT  url_img
FROM
(
    SELECT url_img, object_id FROM images 
    INNER JOIN products ON images.object_id = products.id 
    WHERE images.object = "product"
) AS t1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question