列取决于其他列 [英] Column Depending on other column

查看:92
本文介绍了列取决于其他列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个mysql表名'members',其中付款和ewallet是两列...我想做ewallet列,这样当付款将更新时它会自动更新.规则将是ewallet=0.75*payment .怎么可能?

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?

还有另一列'enroller_id',通过该列我可以使用php函数计算左计数和右计数下线

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.

推荐答案

您可以使用 generated columns (MySQL 5.7.6 +):

You could use generated columns (MySQL 5.7.6+):

生成的列的值是根据列定义中包含的表达式计算的.

生成的列定义具有以下语法:

Generated column definitions have this syntax:

col_name data_type [GENERATED ALWAYS] AS(表达式)[VIRTUAL | 存储] [唯一[关键字]] [评论注释] [[不]空] [[主] KEY]

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

这篇关于列取决于其他列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆