写一个mysql语句,更新d列,当a列大于等于9分显示满意,小于9分为不满意,空值返回空值
时间: 2023-06-25 11:04:24 浏览: 98
假设表名为`table_name`,可以使用以下MySQL语句实现更新d列:
```
UPDATE table_name
SET d = CASE
WHEN a >= 9 THEN '满意'
WHEN a < 9 THEN '不满意'
ELSE NULL
END
WHERE a IS NOT NULL;
```
其中,使用了`CASE`语句对不同情况进行处理。其中,`a`列大于等于9分时,`d`列会被更新为`满意`,小于9分时,`d`列会被更新为`不满意`,如果`a`列的值为`NULL`,则`d`列会被更新为`NULL`。此外,在`WHERE`子句中排除了`a`列为`NULL`的情况,避免了更新无效数据。
相关问题
写一个mysql语句,更新d列,当a列大于等于9分小于10显示满意,小于9分为不满意,空值返回空值,其他等于本身
假设表名为 `table1`,可以使用以下 MySQL 语句实现更新:
```
UPDATE table1 SET d =
CASE
WHEN a >= 9 AND a < 10 THEN '满意'
WHEN a < 9 THEN '不满意'
WHEN a IS NULL THEN NULL
ELSE d
END;
```
其中,`CASE` 语句会根据 `a` 列的值进行判断,根据不同条件更新 `d` 列。如果 `a` 列的值为 `NULL`,则更新为 `NULL`。如果不满足以上条件,则将 `d` 列更新为本身。
一列数据中,结果有8,9,8分,9分,满意,不满意等,mysql如何把前四者归为满意,其他归为不满意或空值
可以使用MySQL中的CASE语句来实现。具体操作如下:
```
UPDATE table_name
SET satisfaction =
CASE
WHEN score IN (8,9) THEN '满意'
ELSE '不满意或空值'
END
WHERE satisfaction IS NULL;
```
其中,table_name是你要更新的表名,score是分数列名,satisfaction是你要更新的列名。这个语句将前四个分数对应的结果设置为满意,其余的设置为不满意或空值。注意,这个语句只会更新satisfaction列中原来为空的行,如果要更新所有行,可以去掉WHERE子句。
阅读全文
相关推荐
















