简体   繁体   中英

Sql update from table with multiple rows

We are working on a project that loads images into a table and gives them an itemnumber, imagesize, imagelink. Now we need to merge this table with our inventory table. The inventory table can have 10 different pictures per item but in our image table we have only 3 rows. There are multiple itemnumbers in this table each with a different images and sizes. How can we update table1 with the links from table2 and have them fill in all 10 pictures?

update table1
 if (imglink is not null)
    set
  imgsize1 = c.imagesize,
  imglink1= c.imageLink
  else if (imglink1 is not null)
  set
  imgsize2 = c.imagesize
  imglink2= c.imageLink
  else if (imglink2 is not null)
  set
  imgsize3 = c.imagesize
  imglink3 = c.imageLink
  else if (imglink3 is not null)
  set
  imgsize4 = c.imagesize
  imglink4 = c.imageLink
  else if (imglink4 is not null)
  set
  imgsize5 = c.imagesize
  imglink5 = c.imageLink
  else if (imglink5 is not null)
  set
  imgsize6 = c.imagesize
  imglink6 = c.imageLink

  else if (imglink6 is not null)
  set
  imgsize7 = c.imagesize
  imglink7 = c.imageLink
  else if (imglink7 is not null)
  set
  imgsize8 = c.imagesize
  imglink8 = c.imageLink
  else 
  set
  imagesize = c.imagesize
  imagelink = c.imageLink


from table2 as c
 WHERE c.itemno = i.itemno

Does what we are trying to do make sense? Any help would be appreciated. Thanks in advance.

Edit: We are using MS SQL 2008. The image table was imported from one of our clients and that was the format it was given. Just a big table with 3 columns. Itemno, imagesize, imageLink.

Sample data: table1 (inventory table)-

itemno | imageLink  | imageLink1 | imageLink2 | imageLink3 | imageLink4 | imageLink5 | imageLink6 | imageLink7 | imageLink8 | imageLink9

table2 (image table)-

itemno | imageLink  | size
itemno | imageLink1 | size
itemno | imageLink2 | size
itemno | imageLink3 | size
itemno | imageLink4 | size
itemno | imageLink5 | size
itemno | imageLink6 | size
itemno | imageLink7 | size
itemno | imageLink8 | size
itemno | imageLink9 | size

You can't put IF around SET in an UPDATE query. The way you set a column conditionally is with:

SET <colName> = IF(<condition>, <newvalue>, <colName>)

If the condition is true, it changes the value, otherwise it just assigns the current value back to the column.

To update one table based on another, the general syntax is:

UPDATE table1 t1
JOIN table2 t2 ON t1.itemno = t2.itemno
SET t1.col1 = <expression which may reference t2>,
    t1.col2 = <expression which may reference t2>,
    ...
WHERE ... /* this clause is optional */

I don't understand the specific logic of your update, but hopefully this should be enough to allow you to translate what you want into correct syntax. If not, add your updated query to the question and we'll try to get you going.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM