如何使用PDO将MySQL Case Case语句插入PHP,以便可以将输出添加到柱形图中

时间:2019-03-30 15:15:52

标签: pdo

我正在处理柱形图,并希望将MySQL Case Case Statement的值添加到图表中。但是,我需要帮助才能使用PDO在PHP中编写该语句,以便将其集成到应用程序的柱形图中。如何使用PDO将Case when语句写入PHP?

SELECT

COUNT(CASE WHEN tbl_projects.projsector = 'Agriculture' THEN 1 END) AS `AG`,     

COUNT(CASE WHEN tbl_projects.projsector = 'Building Construction' THEN 1 END) AS `BC`,    

COUNT(CASE WHEN tbl_projects.projsector = 'Health' THEN 1 END) AS `HT`,    

COUNT(CASE WHEN tbl_projects.projsector = 'Education' THEN 1 END) AS `ED`,  

COUNT(CASE WHEN tbl_projects.projsector = 'Water Supply' THEN 1 END) AS `WS`,  

COUNT(CASE WHEN tbl_projects.projsector = 'Income Generation' THEN 1 END) AS `IG`,  

COUNT(tbl_projects.projsector) AS 'Total SEctors',  

concat(round(COUNT(CASE WHEN tbl_projects.projsector = 'Agriculture' THEN 1 END)/COUNT(tbl_projects.projsector) * 100 )) AS '% AG', 

concat(round(COUNT(CASE WHEN tbl_projects.projsector = 'Building Construction' THEN 1 END)/COUNT(tbl_projects.projsector) * 100)) AS '% BC',

concat(round(COUNT(CASE WHEN tbl_projects.projsector = 'Health' THEN 1 END)/COUNT(tbl_projects.projsector) * 100)) AS '% HT', concat(round(COUNT(CASE WHEN tbl_projects.projsector = 'Education' THEN 1 END)/COUNT(tbl_projects.projsector) * 100 )) AS '% ED', 

concat(round(COUNT(CASE WHEN tbl_projects.projsector = 'Water Supply' THEN 1 END)/COUNT(tbl_projects.projsector) * 100)) AS '% WS',

concat(round(COUNT(CASE WHEN tbl_projects.projsector = 'Income Generation' THEN 1 END)/COUNT(tbl_projects.projsector) * 100)) AS '% IG'

FROM tbl_projects

我的预期结果应该是这样

AG  BC HT ED WS IG TotalSEctors  %AG  %BC %HT %ED %WS %IG    
2   1  0  2  3  0      8          25  13   0  25  38   0

提前谢谢

1 个答案:

答案 0 :(得分:0)

感谢所有尝试为我的问题提供帮助的人。我终于解决了我的问题。我将在下面发布我使用的代码。

现在我的图表需要两个方面的值。第一个是从AG到IG的正常COUNT值,下面是从%AG到%IG的百分比值。

AG  BC HT ED WS IG TotalSEctors  %AG  %BC %HT %ED %WS %IG    
2   1  0  2  3  0      8          25  13   0  25  38   0

要从表中生成此值,我在线进行了挖掘,找到了一些有关如何使用PDO Prepared Statement在MySQL中计算值的代码。学习了代码之后,我可以如下所示编写我的

   <?php

    //database connection

    include('Connections/db.php');

    //create a function for count

    function rowCount($connect,$query){

        $stmt = $connect->prepare($query);

    $stmt->execute();

    return $stmt->rowCount();

    }

    ?> 

使用这一行代码,我能够对记录进行计数,然后生成百分比。

<h1> Agriculture = `<?php echo (rowCount($connect,"SELECT projsector AS '% AG' FROM 
tbl_projects WHERE projsector = 'Agriculture' ") /  rowCount($connect,"SELECT 
projsector FROM tbl_projects ")) * 100; ?>` </h1>

<h1> Building Construction = `<?php echo (rowCount($connect,"SELECT projsector AS '% 
AG' FROM tbl_projects WHERE projsector = 'Building Construction' ") /  
rowCount($connect,"SELECT projsector FROM tbl_projects ")) * 100; ?> </h1>

<h1> Health = `<?php echo (rowCount($connect,"SELECT projsector AS '% AG' FROM 
tbl_projects WHERE projsector = 'Health' ") /  rowCount($connect,"SELECT projsector 
FROM tbl_projects ")) * 100; ?>` </h1>

<h1> Education = `<?php echo (rowCount($connect,"SELECT projsector AS '% AG' FROM 
tbl_projects WHERE projsector = 'Education' ") /  rowCount($connect,"SELECT 
projsector FROM tbl_projects ")) * 100; ?>` </h1>   

<h1> Water Supply = `<?php echo (rowCount($connect,"SELECT projsector AS '% AG' FROM 
tbl_projects WHERE projsector = 'Water Supply' ") /  rowCount($connect,"SELECT 
projsector FROM tbl_projects ")) * 100; ?>` </h1> 

<h1> Income Generation = `<?php echo (rowCount($connect,"SELECT projsector AS '% AG' 
FROM tbl_projects WHERE projsector = 'Income Generation' ") /  
rowCount($connect,"SELECT projsector FROM tbl_projects ")) * 100; ?>` </h1>

输出

Agriculture = 25%
Building Construction = 12.5%
Health = 0%
Education = 25%
Water Supply = 37.5%
Income Generation = 0%

对于没有百分比的正常COUNT值,我使用这些代码

Agriculture = <?php echo rowCount($connect,"SELECT projsector AS '% AG' FROM 
tbl_projects WHERE projsector = 'Agriculture' "); ?>   

Building Construction = <?php echo rowCount($connect,"SELECT projsector AS '% BC' 
FROM tbl_projects WHERE projsector = 'Building Construction' "); ?>

Health = <?php echo rowCount($connect,"SELECT projsector AS '% HT' FROM tbl_projects 
WHERE projsector = 'Health' "); ?>   

Education = <?php echo rowCount($connect,"SELECT projsector AS '% ED' FROM 
tbl_projects WHERE projsector = 'Education' "); ?>

Water Supply = <?php echo rowCount($connect,"SELECT projsector AS '% WS' FROM 
tbl_projects WHERE projsector = 'Water Supply' "); ?>  

Income Generation = <?php echo rowCount($connect,"SELECT projsector AS '% IG' FROM 
tbl_projects WHERE projsector = 'Income Generation' "); ?> 

我相信有人会发现这些有用