1

Is it possible to create a generated MySQL column that runs a condition on other columns?

For example: column "a" - type boolean column "b" - type date generate column "c" that implements the logic:

if (a == false) || (a == true && b < NOW()) {
    return true;
} else {
    return false;
}
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
shugigo
  • 133
  • 1
  • 10

2 Answers2

2

You can use IF/ELSE (https://dev.mysql.com/doc/refman/5.7/en/if.html) or CASE/WHEN (https://dev.mysql.com/doc/refman/5.7/en/case.html) statements in MySQL.

If you're writing a procedure, you can save the output of a query to a variable and check against that. If you aren't doing a procedure, you could do the validation inside of the statement and that should work.

Good luck!

brandoncluff
  • 303
  • 1
  • 5
  • 19
0

You can write scripts for databases. I've done it in Ruby but I'm sure there are plenty of others. You would write a script to create the table without the conditional column and then you would write logic to add a new column with values based on your conditions.

Jared
  • 100
  • 8