Question

I need to make the default value in img_path column equal to images/defaultactive.png if the row has an isActive value equal to 1. As you can see in the picture, the default value for the img_path is the same regardless of value of isActive column.

I have tried this but get syntax error:

ALTER TABLE my_table ALTER COLUMN img_path SET DEFAULT 'images\/defaultactive.png' WHERE isActive = 1;

Is this possible to set this rule? again the default value for img_path column needs to be dependent on the value of isActive column (either 0 or 1). img_path needs to be equal to images/soldico1.png if isActive = 0 and equal to images/defaultactive.png if isActive = 1.

I am new to MySQL so please keep as simple as possible!

Était-ce utile?

La solution

There is no such feature in Mysql. You best doing this 'logic' in the application that inserts the row.

Or you could have the default 'empty' and then the 'display' code makes the decision what to use

if (empty($img_path)) 
   $img_path = ($isActive)?'active.png':'solico.png';

Another reason to do this at 'display' time, rather than in the database. If the active column changes, persumably the default would have change, so it would make that 'update' statement rather messy, doing at display time, avoids having to keep the database in sync.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top