在 CI activerecord 上将结果中的两行或多行连接成单个结果

Concanate two or more rows from result into single result on CI activerecord

我有这样的情况,我想根据我选择的月份和年份从多个行中获取数据库的值(此值使用逗号分隔),有关更多详细信息,请查看此内容..

我的Schedule.sql:

+---+------------+-------------------------------------+
|ID |Activ_date  | Do_skill                            |
+---+------------+-------------------------------------+
| 1 | 2020-10-01 | Accountant,Medical,Photograph       |
| 2 | 2020-11-01 | Medical,Photograph,Doctor,Freelancer|
| 3 | 2020-12-01 | EO,Teach,Scientist                  |
| 4 | 2021-01-01 | Engineering, Freelancer             |
+---+------------+-------------------------------------+

我的skillqmount.sql:

+----+------------+------------+-------+
|ID  |Date_skill  |Skill       |Price  |
+----+------------+------------+-------+
| 1  | 2020-10-02 | Accountant | $ 5   |
| 2  | 2020-10-03 | Medical    | $ 7   |
| 3  | 2020-10-11 | Photograph | $ 5   |
| 4  | 2020-10-12 | Doctor     | $ 9   |
| 5  | 2020-10-01 | Freelancer | $ 7   |
| 6  | 2020-10-04 | EO         | $ 4   |
| 7  | 2020-10-05 | Teach      | $ 4   |
| 8  | 2020-11-02 | Accountant | $ 5   |
| 9  | 2020-11-03 | Medical    | $ 7   |
| 10 | 2020-11-11 | Photograph | $ 5   |
| 11 | 2020-11-12 | Doctor     | $ 9   |
| 12 | 2020-11-01 | Freelancer | $ 7   |
+----+------------+------------+-------+

在我的网站中,我想用这两个 table 进行计算。因此,如果在我的网站上想要查看从日期 2020-10-01 到 2020-11-01 之间的总金额,我会尝试使用以下代码显示它:

输出示例

+----+-----------+-----------+---------+
|No  |Date Start |Date End   |T.Amount |
+----+-------- --+-----------+---------+
|1   |2020-10-01 |2020-11-01 |$ 45     | <= this amount came from ++++++
+----+-------- --+-----------+---------+

Note : 
Date Start : Input->post("A")
Date End : Input->post("B")
T.Amount : Total Amount based input A and B (on date)

我试过这个代码来获取它:

<?php 
$startd = $this->input->post('A');
$endd= $this->input->post('B');
$chck = $this->db->select('Do_skill')
                    ->where('Activ_date >=',$startd)
                    ->where('Activ_date <',$endd)
                    ->get('Schedule')
                    ->row('Do_skill');
$dcek = $this->Check_model->comma_separated_to_array($chck);
$t_amount = $this->db->select_sum('price')
                    ->where('Date_skill >=',$startd)
                    ->where('Date_skill <',$endd)
                    ->where_in('Skill',$dcek)
                    ->get('skillqmount')
                    ->row('price');

echo $t_amount; ?>

Check_model :

public function comma_separated_to_array($chck, $separator = ',')
{
  //Explode on comma
  $vals     =   explode($separator, $chck);
  $count    =   count($vals);
  $val      =   array();
  //Trim whitespace
  for($i=0;$i<=$count-1;$i++) {
    $val[]   .=   $vals[$i];
  }
  return $val;
}

我的问题是 $t_amount 的结果而不是 $45,我认为我上面的代码有一些遗漏,如果有任何建议,我非常感谢...谢谢...

数组 $dcek 的值为

Accountant,Medical,Photograph

来自 Codeigniter 的查询是

SELECT SUM(`price`) AS `price` FROM `skillqmount` 
WHERE `Date_skill` >= '2020-10-01' AND 
`Date_skill` < '2020-11-01' AND 
`Skill` IN('Accountant', 'Medical', 'Photograph')

其中 returns 17 - 这与您数据中的前三个条目匹配。

即使日期范围匹配多行,您的第一个查询也只会给出一行。

你的第一个查询只有return1行数据。

我认为您可以对第一个查询执行类似的操作。

$query1 = $this->db->query("SELECT Do_skill FROM schedule WHERE activ_date >= $startd and  activ_date < $startd");
        $check = $query1->result_array();
        $array = [];

        foreach($check as $ck){
            $dats = explode(',',$ck['Do_skill']);
            
            $counter = count($dats);

            for($i=0;$i<$counter;$i++){
                array_push($array,$dats[$i]);
            }

您可以使用该数组进行下一个查询:)