如何使用子查询进行进一步计算?

时间:2014-07-23 07:28:26

标签: mysql sql calculated-columns

如何创建子查询以进一步计算DATEDIFF,包括另一列(金额),并为DATEDIFF和X提供输出?

例如。的 ddate*amount/365*0.1 = X

$result = mysqli_query($con,"SELECT *,
          DATEDIFF(mdate,pdate)
          AS ddate 
          FROM contacts
          WHERE DATEDIFF(mdate,pdate)");

while($row = mysqli_fetch_array($result)) {
    echo "<td>" . $row['ddate'] . "</td>";
    }

1 个答案:

答案 0 :(得分:0)

你可以直接做,不需要子查询:

 $result = mysqli_query($con,"SELECT DATEDIFF(mdate,pdate) as NoOfDays,
      (DATEDIFF(mdate,pdate) * amount) / 365 * 0.1 as XAmount
      FROM contacts
      WHERE DATEDIFF(mdate,pdate)>=1");