我在用PHP制作动态图形时有点卡住了。下面是代码:
$arrlength = count($ages);for($x = 0; $x < $arrlength; $x++) {
$query = $con->query("SELECT SUM(Amount) AS AmountTotal FROM Col WHERE Type!=1 AND client_id = $client_id AND (StartAge<$ages[$x] OR Start=$ages[$x]) AND (EndAge>$ages[$x] OR EndAge=$ages[$x])");
if(mysqli_num_rows($query)>0) {
while($row = $query->fetch_assoc()){
$Total = $row['AmountTotal'];
}
}
if(!empty($Total)){echo" $Total,";}else { echo" 0,";}
}
图形x轴为Age, Y为Sum of Amount。它们在数据中的多个条目。
数据为:
你会运行更少的查询,并有一个更有效的过程,如果你考虑总结你的查询结合你的逻辑表达式(即使用<=代替
- 步骤1 -使用php生成所有年龄的子查询
$agesTable="( ";
foreach($ages as $age){
$agesTable.=" SELECT $age as age UNION ALL ";
}
$agesTable.=" SELECT NULL as age) ages";
- 创建完整的SQL查询以运行
我假设$client_id
不是从面向客户端的应用程序(例如web/移动应用程序)收到的值,在这种情况下,您应该考虑SQL注入并在运行查询时使用准备好的语句。有很多文章和S/O问题都在讨论这个问题。
$combinedQuery = "SELECT ages.age as Age, SUM(COALESCE(Col.Amount,0)) as AmountTotal FROM $agesTable LEFT JOIN Col ON ages.age BETWEEN Col.StartAge AND Col.EndAge AND Type!=1 AND client_id = $client_id WHERE ages.age IS NOT NULL GROUP BY 1";
- 步骤3 -运行查询
$results = []; //store all your graph data as
if(mysqli_num_rows($query)>0) {
while($row = $query->fetch_assoc()){
if(empty($row['Age']))continue; //optional - to ignore null groups
//append data to your array of results
array_push($results,[
//Below I use 'Age' and 'Total' as my array keys but you may also use X and Y instead
'Age'=>$row['Age'], //extract the age or your 'X' value
'Total'=>$row['AmountTotal'] //extract the total or your 'Y' value
]);
//instead of appending the data for your chart to an array to be used later, you could continue with your current approach and output it here.
}
}
基于年龄的值,您的结果数组看起来像这样。
[
[
'Age'=>1,
'Total'=>10
],
[
'Age'=>2,
'Total'=>10
],
...etc
]
我已经复制了这个解决方案,并使用php和db-fiddle生成查询。您将在下面看到完整的结果
测试PHP代码
<?php
$ages = [];
for($i=1;$i<=10;$i++)$ages[]=$i;
$agesTable="( ";
foreach($ages as $age){
$agesTable.=" SELECT $age as age UNION ALL ";
}
$agesTable.=" SELECT NULL as age) ages";
$client_id=1;
$combinedQuery = "SELECT ages.age as Age, SUM(COALESCE(Col.Amount,0)) as AmountTotal FROM $agesTable LEFT JOIN Col ON ages.age BETWEEN Col.StartAge AND Col.EndAge AND Type!=1 AND client_id = $client_id WHERE ages.age IS NOT NULL GROUP BY 1";
echo $combinedQuery;
使用$combinedQuery
输出的DB Fiddle输出
CREATE TABLE Col (
`StartAge` INTEGER,
`EndAge` INTEGER,
`Amount` INTEGER,
`client_id` INTEGER DEFAULT 1,
`Type` INTEGER DEFAULT 2
);
INSERT INTO Col
(`StartAge`, `EndAge`, `Amount`)
VALUES
('1', '9', '10'),
('5', '7', '10'),
('7', '9', '10');
查询# 1
SELECT
ages.age as Age,
SUM(COALESCE(Col.Amount,0)) as AmountTotal
FROM (
SELECT 1 as age UNION ALL
SELECT 2 as age UNION ALL
SELECT 3 as age UNION ALL
SELECT 4 as age UNION ALL
SELECT 5 as age UNION ALL
SELECT 6 as age UNION ALL
SELECT 7 as age UNION ALL
SELECT 8 as age UNION ALL
SELECT 9 as age UNION ALL
SELECT 10 as age UNION ALL
SELECT NULL as age) ages
LEFT JOIN
Col ON ages.age BETWEEN Col.StartAge AND Col.EndAge AND
Type!=1 AND client_id = 1
WHERE ages.age IS NOT NULL
GROUP BY 1;
Age | AmountTotal | 1 | 10 | 2
---|---|
10 | |
3 | 10 |
10 | |
20 | |
20 | |
30 | |
20 | |
9 | 20 |
0 |
相关内容
- 为什么不是正则内容:在交叉轴上工作之间的空间
- 引导程序在不在标头中工作之间调整内容
- 在 Python 中不可变的字符串和以下代码工作之间,我缺少什么明显的东西? 饶了我吧,因为我对 Python 很陌生
- 如何在周期工作流中的早期版本和当前工作之间共享修复
- 竹子在工作之间共享大型文物
- 在我的HTML页面中,在不工作之间调整内容
- 如何在工作之间共享任务
- 在两个工作之间共享表
- 随着时间的推移,工作之间的射击工作派遣者间隔会增加
- 在代码签名符中不工作之间
- 在工作之间传递容器
- 如何在Gitlab-Runner工作之间共享工件,而无需公开下载
- 任务和任务工作之间的区别
- 春季批次如何共享工作之间的数据
- 如果日期跨越多年,则介于不工作之间的日期
- 在创建线程后相机不工作之间切换
- 工作之间的区别是什么?提交和作业.Apache Hadoop中的waitForComplete
- 如何找到工作之间的反向关系
- 使用 >= 、<、> 甚至不在 mysql 存储过程中工作但作为单个查询工作之间的日期比较
- 如何处理工作之间的竞争条件,例如beanstalk
最新更新
- 如何在启动GCP VM实例时使用自定义python命令调度该实例?
- 图像消失时,我选择一个文件与Flet Python
- 是否有一种方法允许CORS处理飞行前响应的自定义标头
- OSError: [Errno 57] Socket is not connected (python macos中的S
- 使用lag()或类似的方法,具有开始值和结束值的帐户滚动
- 我如何重写子类中重写超类的函数依次(Python)?
- 无法使用WooCommerce API将产品添加到相应的类别/子类别
- 用户登录到应用程序注册时Azure AD管理员同意错误
- Unity 3d:在不知道我将使用的确切预制的情况下,我如何在运行时启动预制?
- c -构建简单shell时文件重定向的问题
- AWS Lambda实际上记录到控制台以外的任何本地接收器(因此是CloudWatch)吗?
- 我如何在Django中为mymyy输入一个自定义用户模型管理器?
- discord.js v13交互按钮删除原始消息
- 在Flutter中默认禁用BlocBuilder中的TextButton
- 如果数据已经被获取,如何防止Redux工具包中的加载状态
- Javascript:在深度嵌套的数组和对象中查找匹配的属性值
- 将本地时间转换为用户首选时区,将用户首选时区转换为GMT
- 为什么 AWS Web 应用程序的前端和后端在这些关系图中直接连接?
- 是否有一个顺序Java正则表达式?
- 如何向右移动一个字符串n个字符
- 我应该从存储库返回任务<IEnumerable<T>>还是IAsyncEnumerable<T>?
- Python处理程序错误-没有足够的值来解包
- 使用 javascript 获取最接近和最高的 id
- 如何利用向量化技术提高MATLAB程序的效率
- 如何将PostgreSQL查询转换为LINQ或lambda表达式
- Python mysql-connector-python:如何使用mysql-connector-python从终端连
- 新的json数据不显示在android
- 如果不同时更改当前头像,则无法更新用户详细信息
- Kotlin HTTP GET 数据并插入到文本视图中
- 滑块在移动设备上不跟随手指
热门标签:
javascript python java c# php android html jquery c++ css ios sql mysql arrays asp.net json python-3.x ruby-on-rails .net sql-server django objective-c excel regex ruby linux ajax iphone xml vba spring asp.net-mvc database wordpress string postgresql wpf windows xcode bash git oracle list vb.net multithreading eclipse algorithm macos powershell visual-studio image forms numpy scala function api selenium