I need to set a field value, actually a flag, in a record to either 0 or 1 depending on whether the id field (int(10) auto_increment, primary key) is even or odd. I know I can determine the value after insertion with a bitwise selector 开发者_如何学Pythonand then run an update but I'm wondering if there is a way to do this within the insert statement.
I think the best way is to set a trigger on the insert action that will automaticaly set the parity of that field.
Why use another field to store data your ID is already carrying?
Even:
SELECT * FROM my_table WHERE (MOD(id,2) = 0)
Odd:
SELECT * FROM my_table WHERE (MOD(id,2) > 0)
精彩评论