如果列不为NULL,如何更新表中的列?

时间:2017-07-17 09:30:19

标签: php mysql

我有一个将数据提交到表格的表单。除了received_dategid之外,其他用户可以修改表格列。如何在PHP的更新脚本中跳过列?

这是default_table(简化):

gid     received_date     detail     max_number   status
1                NULL       NULL          NULL         1

首次输入后:

gid     received_date         detail     max_number   status
1          2017-07-17       Swimsuit            100        1

在第二次输入后依次输入:

gid     received_date          detail     max_number   status
1          2017-07-17     Swimsuit XL            250        1

可能是这样的:

UPDATE t_goods 
IF(t_goods.received_date = NULL){
   SET received_dates = received_date(today_date), detail = detail, max_number = add_stock WHERE gid = gid
}
ELSE{
   SET detail = detail, max_number = add_stock WHERE gid = gid
}

注意:查询只是一个伪代码。

2 个答案:

答案 0 :(得分:2)

尝试那样。

UPDATE t_goods 
SET 
detail = detail, 
max_number = add_stock,
received_date = IF(received_date IS NULL, received_dates,received_date)
WHERE  gid = gid;

IF(received_date IS NULL, received_dates,received_date)如果received_date为null,则此等式更新new received_dates值,否则将receive_date保留为旧值。

答案 1 :(得分:-1)

UPDATE t_goods SET received_dates = NOW(), detail = x, max_number = add_stock WHERE gid = gid AND t_goods.received_date IS NULL;

很难理解更多信息,这个怎么样?