以下是我的代码:
$sql = "SELECT count(EventCode) as Total_Event FROM sample where Age between 5 and 10";
$stmt = sqlsrv_query( $conn, $sql )
$sql1 = "SELECT count(EventCode) as Total_Event FROM sample where Age between 50 and 80";
$stmt1 = sqlsrv_query( $conn, $sql1 );
从上面的查询中,我可以计算sql
和sql1
语句发生的事件数。
我需要对这两个查询值sql
和sql1
请帮助我如何更改我的代码以实现它。
答案 0 :(得分:2)
您希望两个不同年龄段的人的平均事件数量。这真的很奇怪,但最简单的方法是单个查询:
SELECT count(EventCode) / 2 as strange_average_calculation
FROM sample
WHERE Age between 5 and 10 OR Age between 50 and 80;
答案 1 :(得分:0)
你可以使用sql函数,即SELECT AVG(column_name)
$sql = "SELECT AVG(EventCode) as avg_Event FROM sample where Age between 5 and 10";
$stmt = sqlsrv_query( $conn, $sql )
$sql1 = "SELECT AVG(EventCode) as avg_Event_2 FROM sample where Age between 50 and 80";
$stmt = sqlsrv_query( $conn, $sql1 );