开发者

MySQL insert with value determined at insertion

开发者 https://www.devze.com 2023-04-06 17:55 出处:网络
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 afte

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消