Mysql选择带嵌套的If和count

时间:2018-09-28 15:18:32

标签: php mysql

需要有关我的代码的帮助,对不起,我只是在此处发布完整的查询信息的简短摘要

SELECT si_visit.company, IF(si_visit.company='SIPES',COUNT(IF(si_visit.visit_type='Regular',1, NULL)),COUNT(IF(si_visit.visit_type='Regular',1, NULL))) 'Regular',
IF(si_visit.company='SIPES',COUNT(IF(si_visit.visit_type='Installation',4, NULL)),COUNT(IF(si_visit.visit_type='Installation',1, NULL))) 'Installation', 
IF(si_visit.company='SIPES',COUNT(IF(si_visit.visit_type='Emergency',4, NULL)),COUNT(IF(si_visit.visit_type='Emergency',1, NULL))) 'Emergency', 
IF(si_visit.company='SIPES',COUNT(IF(si_visit.visit_type='Cancellation',1, NULL)),COUNT(IF(si_visit.visit_type='Cancellation',1, NULL))) 'Cancellation', 
IF(si_visit.company='SIPES',COUNT(IF(si_visit.visit_type='Remotely',1, NULL)),COUNT(IF(si_visit.visit_type='Remotely',1, NULL))) 'Remotely', 
COUNT(*) AS Total 
FROM si_visit INNER JOIN si_visit_type WHERE si_visit_type.visit_name= si_visit.visit_type  
GROUP BY si_visit.company

我需要的是如果公司名称为SIPES,则将“紧急安装”计算为4,其他计算为1 而php代码是

$statement = $connection->prepare($query);
$statement->execute();
$filtered_rows = $statement->rowCount();
$statement = $connection->prepare($query . $query1);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
foreach($result as $row){
$sub_array = array();
//$sub_array[] = $nub;
$sub_array[] = $row["company"];
$sub_array[] = $row["Regular"];
$sub_array[] = $row["Emergency"];
$sub_array[] = $row["Remotely"];
$sub_array[] = $row["Installation"];
$sub_array[] = $row["Cancellation"];
$sub_array[] = $row["Total"];
$data[] = $sub_array;
}
echo json_encode($data);

我的数据库表是:

CREATE TABLE `si_visit` (
`id` int(11) NOT NULL,
`c_id` int(11) NOT NULL,
`visit_type` varchar(255) NOT NULL,
`engineer_name` varchar(255) NOT NULL,
`company` varchar(255) NOT NULL,
`notes` text NOT NULL,
`visit_date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Visits table for Clients';

CREATE TABLE `si_visit_type` (
`id` int(11) NOT NULL,
`visit_name` varchar(255) NOT NULL,
`visit_count` int(11) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Visit type and count for it';
INSERT INTO `si_visit_type` (`id`, `visit_name`, `visit_count`) VALUES
(1, 'Regular', 1),
(2, 'Installation', 4),
(3, 'Emergency', 4),
(4, 'Cancellation', 1),
(5, 'Remotely', 1);

当我对其进行查询时,它全部计为1我尝试了很多方法都给了我错误 将MySQL与MariaDB结合使用

我做错了什么? 请你能不能帮忙

1 个答案:

答案 0 :(得分:0)

谢谢大家的帮助, 我只是想出我使用了计数,那就是计数行 我只是将其更改为总和就可以正确显示 谢谢大家。

  SELECT si_visit.company,
 IF(si_visit.company='SIPES',SUM(IF(si_visit.visit_type='Regular',1, 0)),SUM(IF(si_visit.visit_type='Regular',1, 0))) 'Regular',
IF(si_visit.company='SIPES',SUM(IF(si_visit.visit_type='Installation',4, 0)),SUM(IF(si_visit.visit_type='Installation',1, 0))) 'Installation',
IF(si_visit.company='SIPES',SUM(IF(si_visit.visit_type='Emergency',4, 0)),SUM(IF(si_visit.visit_type='Emergency',1, 0))) 'Emergency',
IF(si_visit.company='SIPES',SUM(IF(si_visit.visit_type='Cancellation',1, 0)),SUM(IF(si_visit.visit_type='Cancellation',1, 0))) 'Cancellation',
IF(si_visit.company='SIPES',SUM(IF(si_visit.visit_type='Remotely',1, 0)),SUM(IF(si_visit.visit_type='Remotely',1, 0))) 'Remotely', COUNT(*) AS Total
FROM si_visit INNER JOIN si_visit_type WHERE si_visit_type.visit_name= si_visit.visit_type GROUP BY si_visit.company