我的sql中有这个表,我希望使用cus_count1
值来减去cus_count2
值。我想查询它,但我不知道如何去做。
+---------------------+------------+------------+ | Time | cus_count1 | cus_count2 | +---------------------+------------+------------+ | 2015-12-21 08:28:44 | 1 | 0 | | 2015-12-21 08:29:09 | 2 | 1 | | 2015-12-21 08:29:23 | 3 | 1 | | 2015-12-21 08:29:35 | 4 | 1 | | 2015-12-21 08:29:49 | 4 | 2 | | 2015-12-21 08:30:09 | 5 | 2 | | 2015-12-21 08:30:17 | 6 | 2 | +---------------------+------------+------------+
这是我的PHP代码:
<?php
$server = "localhost";
$user = "root";
$password = "";
$database = "";
$connection = mysql_connect($server,$user,$password);
$db = mysql_select_db($database,$connection);
$query1 = "SELECT time, cus_count1 - cus_count2 AS cus_flow FROM new_tbl_cus";
$result1 = mysql_query($query1);
while($row = mysql_fetch_assoc($result1))
{
$dataset1[] = array(strtotime($row['time'])*1000,$row['cus_flow']);
//echo strtotime($row['time'])*1000;
}
?>
答案 0 :(得分:0)
你想要简单的减法吗?如果是,您可以这样做:
select time, cus_count1 - cus_count2 as finalvalue
from tablename
我想你可能会要求一些不同的东西,或者这就是你要求的东西?在上面的语句中,finalvalue是给予计算/派生列的人工列名(aka别名)。
答案 1 :(得分:0)
使用
SELECT *, cus_count1 - cus_count2 as diff FROM [dbo].tablename
作为SQL。
您将获得一个额外的列Diff作为结果集中的最后一列。
如果要减去特定行,请在表的主键或后续表列中添加 WHERE 子句