MySql更新案例不能为空

时间:2017-03-14 15:14:32

标签: mysql case

我的案例更新存在问题

我有一张名为diccionary的表

+-----------------------------------------------------------------------+
| id   |  table_name   |  col_name |  size    |   precis   | data_type  |
+------+---------------+-----------+----------+------------+------------+
|  1   |  'user'       |  'id'     |  null    |   10       |  'int'     |
|  2   |  'user'       | 'name'    |  255     |   null     |  'varchar' | 
|  3   |  'user'       |'lastname' |  255     |   null     |  'varchar' |
|  4   |  'user'       | 'age'     |  null    |     2      |  'varchar' |

我尝试使用此查询

UPDATE diccionary SET
size = CASE WHEN id = 2 THEN 250 END,
data_type = CASE when id = 4 THEN 'int' END;

但我收到此错误

#1048 - Column 'data_type' cannot be null 

我的查询有什么问题?感谢

2 个答案:

答案 0 :(得分:2)

data_type列不能为空,所以你只需管理不等于4的情况,例如:assign varchar

UPDATE diccionary 
SET size = CASE WHEN id = 2 THEN 250 END,
data_type = CASE when id = 4 THEN 'int' ELSE 'varchar' END;

答案 1 :(得分:1)

好吧,如果列不为null,则需要一个默认值。尝试添加' ELSE'条件:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="colors" id="colors">
  <option value="red">Red</option>
  <option value="blue">Blue</option>
  <option value="yellow">Yellow</option>
</select>

<select name="textcolor" id="textcolor">
  <option value="black">Black Text</option>
  <option value="white">White Color</option>
</select>

<div id="box">
  Text
</div>