Shota
Shota

Reputation: 513

How to UPDATE a column in a table if the column is not NULL?

I have a form that submit data to a table. The table column is editable by other user except received_date and gid . How to make the column skipped during the update script from PHP?

This is the default_table (simplified):

gid     received_date     detail     max_number   status
1                NULL       NULL          NULL         1

After first input:

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

After second and so on input:

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

is possible something like this:

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
}

note: the query is just a pseudocode.

Upvotes: 2

Views: 94

Answers (2)

Ajay Korat
Ajay Korat

Reputation: 701

Try like that.

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) This equation is update new received_dates value if received_date is null, otherwise it will keep received_date as old value.

Upvotes: 2

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

It's hard to understand with out more information, how about this?

Upvotes: -1

Related Questions