要检索百分比的MySQL表达式
我有一个表(表1),有3列: Name | RegDate | Active 我想知道RegDate列中两个日期之间活动的行(active=1)的百分比是多少 例如: 在“2013-01-01”和“2013-03-31”(RegDate字段)之间,我想知道活动百分比是多少(active=1) 是否可以在单个MySQL表达式中执行此操作 目前,我有以下几点: $sql=“从表1 t中选择*,其中t.RegDate='2013-01-01' $sql\u executa=$conn->prepare($sql)或die($sql) $sql_executa->execute() $total=$sql_executa->rowCount() $sql_2=“从表1 t中选择*,其中t.RegDate='2013-01-01'和Active=1” $sql\u executa\u 2=$conn->prepare($sql\u 2)或die($sql\u 2) $sql_executa_2->execute() $total_2=$sql_executa_2->rowCount() $Percentagem=($total_2*100)/$total;//这就是我需要的答案 提前感谢试试:要检索百分比的MySQL表达式,mysql,Mysql,我有一个表(表1),有3列: Name | RegDate | Active 我想知道RegDate列中两个日期之间活动的行(active=1)的百分比是多少 例如: 在“2013-01-01”和“2013-03-31”(RegDate字段)之间,我想知道活动百分比是多少(active=1) 是否可以在单个MySQL表达式中执行此操作 目前,我有以下几点: $sql=“从表1 t中选择*,其中t.RegDate='2013-01-01' $sql\u executa=$conn->prepare
SELECT sum( case when Active=1 then 1 else 0 end )
/
count(*)
* 100
As Percent_of_Active_records
FROM Table1 t
WHERE t.RegDate <= '2013-03-31'
AND t.RegDate >= '2013-01-01'
选择sum(活动时为1,否则为0结束)
/
计数(*)
* 100
占活动记录的百分比
来自表1 t
其中t.RegDate='2013-01-01'