简体   繁体   中英

Column Depending on other column

I have created a mysql table name 'members' where payment and ewallet are two columns... I want to make ewallet column such that it will automatically update when payment will update.. the rule will be ewallet=0.75*payment . How it is possible?

there is another column 'enroller_id' through which i am calculation the left count and the right count downline with the php function

function getTotalLeg($node,$leg){
$sql="select enrolled_id from members where enroller_id='$node' and tside='$leg' and active='1'";

$res=mysql_query($sql);

global $total;

$total=$total+mysql_num_rows($res);
$row=mysql_fetch_array($res);

 if($row['enrolled_id']!=''){
   getTotalLeg ($row['enrolled_id'],0);
   getTotalLeg ($row['enrolled_id'],1);
  }

return $total;
}

Is it possible to count it in the database? Please help me in detail way. Thanks in advance.

You could use generated columns (MySQL 5.7.6+):

Values of a generated column are computed from an expression included in the column definition.

Generated column definitions have this syntax:

col_name data_type [GENERATED ALWAYS] AS (expression) [VIRTUAL | STORED] [UNIQUE [KEY]] [COMMENT comment] [[NOT] NULL] [[PRIMARY] KEY]

CREATE TABLE members(
   ...,
   ewallet DECIMAL(14,4) AS (0.75 * payment)
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM