简体   繁体   中英

SQL: Update table where column = Multiple Values

I created an SQL query which updates a table column where another column = value

CODE:

Update Products Set ProductName = 'Shoes' WHERE ProductID = (1,2,3,4,5,6,7,8)

The problem is with the ProductID . How can I make it update the column with those ID's?

Regards.

Replace ProductID = with ProductID IN

Update Products 
Set ProductName = 'Shoes' 
WHERE ProductID IN (1,2,3,4,5,6,7,8) 

You just use "IN":

Update Products Set ProductName = 'Shoes' WHERE ProductID in (1,2,3,4,5,6,7,8)

Both answers above are perfectly correct however if there are a list of values in a table you want to use in your IN statement you can use a SELECT statement

...WHERE name IN (select name from listofnames where lastname like 'C%')

I find this to be more of use in a dynamic environment but thought it worth a mention.

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