Jump to content

autosetting a field in a mysql based on the value of another 2 fields in the same table


ajoo
Go to solution Solved by Jacques1,

Recommended Posts

Hi all !, 

 

I was just wondering if it's possible to autoset the value of a field in mysql table based on the value of another 2 fields values.

 

for example if I wish to implement AND logic  on some fields :

 

F1        F2                  F1_F2

========================

0           0                        0

1           0                        0

0           1                        0

1           1                        1

=====================

 

If I have columns F1, F2 &  F1_F2, can the mysql database auto set the value of the F1_F2 based on the values of F1 and F2 as in the table above. If so kindly show how it can be done. Please note that the values of F1 and F2 will be set programmatically using PHP.

 

Thank You.

 

Link to comment
Share on other sites

  • Solution

Why do you need a physical column for the AND operation when the value can simply be derived ad hoc? Precalculating the result is actually a bad idea, because you need to update it whenever any of F1 or F2 change. And if you forget to update it just once, you'll end up with garbage data.

 

Simple calculate the AND result in the query. You could also use a view which contains a virtual F1_F2 column that is calculated from the physical F1 and F2 columns.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.