codeigniter 如何为sql查询添加临时列?

ymzxtsji  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(144)

我有一个SQL查询总结我的表:

$this->where('kriteria =', 'fakir');
    $this->where('zona !=', '0');
    $this->select('surveyor,zona,kriteria,area');
    $this->selectCount('surveyor', 'count');
    $this->selectSum('nilai', 'nilai');
    $this->groupBy('surveyor');
    $this->groupBy('kriteria');
    $this->groupBy('area');
    $this->orderBy('kriteria', 'ASC');
    $this->orderBy('zona', 'ASC');
    $this->orderBy('count', 'DESC');

字符串
输出量:
| 测量师|zona|克里泰里亚|区域|计数|
| --|--|--|--|--|
| RT06 RW07| 1 |Fakir| RUNKUT| 125 |
| RT08 RW07| 1 |Fakir| RUNKUT| 78 |
我需要从查询结果中只取1个数据,但我不能使用测量仪,因为它包含空间。我计划添加可能像id来识别我需要的数据,但我不知道如何,我使用codeigniter 4.4.3
我试着调用id,但它只引用了查询前的一个数据

xzabzqsa

xzabzqsa1#

this->where('kriteria', 'fakir');
$this->where('zona !=', '0');
$this->select('surveyor, zona, kriteria, area');
$this->selectCount('surveyor', 'count');
$this->selectSum('nilai', 'nilai');
$this->select("CONCAT(surveyor, '_', zona, '_', kriteria, '_', area) as custom_id", false);
$this->groupBy('surveyor');
$this->groupBy('kriteria');
$this->groupBy('area');
$this->orderBy('kriteria', 'ASC');
$this->orderBy('zona', 'ASC');
$this->orderBy('count', 'DESC');

$queryResult = $this->get()->getResult();

$selectedRow = null;

foreach ($queryResult as $row) {
    if (strpos($row->custom_id, 'desired_value') !== false) {
        $selectedRow = $row;
        break;
    }
}

This example uses the CONCAT function to concatenate the values of surveyor, zona, kriteria, and area with underscores to create a custom_id for each row. You can adjust the concatenation logic based on your requirements. In the loop, you can then use any logic to select the desired row based on this custom_id or other criteria.

字符串

3df52oht

3df52oht2#

要将其转换为一个完整的SQL查询,它看起来像这样:

SELECT surveyor, zona, kriteria, area, COUNT(surveyor) AS count, SUM(nilai) AS nilai
FROM your_table
WHERE kriteria = 'fakir' AND zona != '0'
GROUP BY surveyor, kriteria, area
ORDER BY kriteria ASC, zona ASC, count DESC;

字符串

相关问题