如何在不同条件下更新一个字段

时间:2011-11-28 02:44:24

标签: mysql

UPDATE `products_description` SET 
`products_name` = 'Name 1' WHERE language_id = 1 AND products_id = 2249,
`products_name` = 'Name 2' WHERE language_id = 2 AND products_id = 2249,
`products_name` = 'Name 3' WHERE language_id = 7 AND products_id = 2249 ;

我需要使用一个查询更新3行。 谢谢!

2 个答案:

答案 0 :(得分:3)

你可以试试这个: -

UPDATE products_description
SET products_name = (
  CASE language_id 
    WHEN 1 THEN 'Name 1'
    WHEN 2 THEN 'Name 2'
    WHEN 7 THEN 'Name 3'
  END
)
WHERE products_id = 2249;

答案 1 :(得分:1)

不确定这是否是mysql的确切语法,但这是我在SQL Server中要做的事情:

UPDATE `products_description` 
   SET `products_name` = CASE WHEN language_id = 1 THEN 'Name 1' 
                              WHEN language_id = 2 THEN 'Name 2' 
                              WHEN language_id = 7 THEN 'Name 3' 
                              ELSE `products_name`
                         END
 WHERE products_id = 2249 ;