使用COUNT DISTINCT显示PDO mysql

时间:2016-08-19 16:56:11

标签: php mysql sql pdo

我正在尝试获取确定的任务类型的数量,但它给了我错误:尝试获取非对象的属性

$statement = $pdo->prepare("SELECT COUNT(DISTINCT Company_Name)
FROM salesplus WHERE `Task_Type` = 'Form Sent'
AND `Consultant_name` = 'Name Surname'");

$statement->execute();
$result=$statement->fetch(PDO::FETCH_OBJ);

foreach($result as $row){
echo $row->Task_Type;

非常感谢你!

1 个答案:

答案 0 :(得分:1)

这不是它的工作原理...尝试(计算确切的任务):

SELECT COUNT(*) as count FROM (SELECT DISTINCT Company_Name FROM salesplus WHERE `Task_Type` = 'Form Sent' AND `Consultant_name` = 'Name Surname') as s

或者更好(获取公司列表和每家公司选定任务的数量):

SELECT `Company_Name`,count(*) as count FROM salesplus WHERE `Task_Type` = 'Form Sent' AND `Consultant_name` = 'Name Surname' GROUP BY `Company_Name`