[英]SQL COUNT one column of table
I have simple script which count how many times users press search button I'm trying to write down SQL command which count all querys for all users with one company. 我有一个简单的脚本,该脚本可以计算用户按下搜索按钮的次数。我试图写下SQL命令,该命令可以计算一家公司的所有用户的所有查询。 Below you see example of my table:
在下面,您可以看到我的表格示例:
login | query | company
user1 | 40 | 1
user2 | 60 | 1
user3 | 30 | 2
So just for example I would like to write statment for how many querys all users from company 1 did, result should be number 100. 因此,举例来说,我想写一份陈述,陈述公司1的所有用户进行了多少次查询,结果应为100。
I try something like this but with no result: 我尝试这样的事情,但没有结果:
public function getCompanyData()
{
try{
$con = new PDO( DB_HOST, DB_USER, DB_PASS );
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
//$sql = "SELECT count(*) as total_count FROM users WHERE company = :company ";
$sql = "SELECT company,SUN(query) as total_count FROM users GROUP BY company";
$stmt = $con->prepare( $sql );
$stmt->bindValue( "company", $this->company, PDO::PARAM_STR );
$stmt->execute();
if($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$this->count = $row['total_count'];
echo $this->count;
return $this->count;
}
}catch( PDOException $e ) {
return $e->getMessage();
}
}
Is there a chance somebody can help me? 有人可以帮助我吗? my own code throwing me error: : Syntax error or access violation: 1305 FUNCTION MyDatabaseName.SUN does not exist
我自己的代码抛出错误::语法错误或访问冲突:1305功能MyDatabaseName.SUN不存在
I'm not even sure if I'm doing it right so any help is welcome. 我什至不确定我是否做对了,因此欢迎任何帮助。
Thanks 谢谢
EDIT: After uergen d help my function work, problem is that if i manualy count all query for company number 1, i get result around 1400, but script show me number around 300 Is there a chance somebody see something what i dont? 编辑:uergen d帮助我的功能工作后,问题是,如果我手动计算公司编号1的所有查询,我会得到大约1400的结果,但是脚本会向我显示大约300的数字有没有机会有人看到我不知道的东西?
Actually it's 其实是
SUM(query)
not 不
SUN(query)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.