dimanche 12 juin 2016

remove or update with empty '' the value from col1 if it's equal to value from col2

I'm looking for a solution to UPDATE (rather empty) the value from sale_price if it's equal to the value of old_price ,and add the text 'lowered' to the prod_title.

sale_price    | old_price | unique_id | prod_title
6.95          | 6.95      | aabb12    | apples 
4.95          | 8.95      | aabb23    | pears 
10.95         | 10.95     | aabb45    | banana's 
30            | 30        | bbdd12    | kiwi's 
15            | 20        | abcd32    | tomatoes 

result

sale_price    | old_price | unique_id | prod_title
              | 6.95      | aabb12    | apples 
4.95          | 8.95      | aabb23    | pears lowered
              | 10.95     | aabb45    | banana's
              | 30        | bbdd12    | kiwi's
15            | 20        | abcd32    | tomatoes lowered

It should be empty, not NULL.

CONCATENATING prod_title with ' Lowered' works fine:

UPDATE tbl set prod_title = CONCAT(prod_title, ' Lowered') WHERE sale_price != old_price ;

Im stuck on removing the value from sale_price if it's equal to old_price:

UPDATE tbl set sale_price = '' WHERE sale_price = old_price ;

returns an error: Error Code: 1265. Data truncated for column 'minimum_price' at row 1

The DATATYPE for sale_price is DOUBLE

Whats the best way to do this? Please keep in mind that i'm about 1 week into mysql so please be gentle.. I appreciate the help.

solution thus far (as a means to an end)

I solved it by changing the DATATYPE to VARCHAR(10) and then running UPDATE tbl set sale_price = '' WHERE sale_price = old_price ;

Worked like a charm, unless there's someone with an answer to do this on a DATATYPE DOUBLE column, which would mean one less query ^^.

Aucun commentaire:

Enregistrer un commentaire