php - 用 php/mysql 中的条件汇总多维数组中的值
问题描述
我在mysql中有下表
e_id | p_id | w1 | w2 | w3 | w4 |
---|---|---|---|---|---|
87 | 1019 | 1 | 1 | 0 | 0 |
87 | 1019 | 0 | 0 | 0 | 1 |
87 | 1020 | 1 | 1 | 1 | 1 |
87 | 1021 | 0 | 1 | 0 | 0 |
87 | 1021 | 0 | 0 | 1 | 1 |
87 | 1021 | 1 | 0 | 0 | 0 |
89 | 1020 | 1 | 1 | 1 | 1 |
89 | 1022 | 1 | 1 | 1 | 0 |
89 | 1022 | 0 | 0 | 0 | 1 |
我想对 e_id 和 p_id 相等的行求和。所以结果应该是这样的:
e_id | p_id | w1 | w2 | w3 | w4 |
---|---|---|---|---|---|
87 | 1019 | 1 | 1 | 0 | 1 |
87 | 1020 | 1 | 1 | 1 | 1 |
87 | 1021 | 1 | 1 | 1 | 1 |
89 | 1020 | 1 | 1 | 1 | 1 |
89 | 1022 | 1 | 1 | 1 | 1 |
我不确定我是否可以在 mysql 中做到这一点,可能只能在 php 中。获取后,我在 php 中得到了以下数组
$schedules[]= array('employee' => $e_id, 'project' => $p_id, 'weeks' => $weeks);
$e_id = fetched e_id from table
$p_id = fetched p_id from table
$weeks = fetched array of w1..w4 from table
我该怎么做?感谢任何帮助
解决方案
select sum(w1) as w1,xxxxxxxxx from table_name group by e_id, p_id
推荐阅读
- ruby-on-rails - Carrierwave:上传前清理 CSV
- javascript - 用正则表达式替换方法
- ios - 在内部类型中实现 Swift 5.1 集合差异
- laravel - csrf-token 在表单提交之前过期
- c++ - 使用带有 PostScript 驱动程序的 GDI+ 的图像质量不佳
- react-native-android - 任务 ':app:transformDexArchiveWithDexMergerForDebug' 执行失败
- mirth - Mirth SQL Server 数据库阅读器源,DBMaxRetries 错误
- node.js - 运行一次性脚本的好方法
- django - Django 站点地图:仅在文档开头允许 XML 声明
- python-3.x - 为python构建C扩展——'undefined reference to _Py_GC_generation0'