<?php
類別 測試 繼承 SQLite3
{
公開 函式 __construct($檔案)
{
父類別::__construct($檔案);
$this->createAggregate('groupConcat', [$this, '合併步驟'], [$this, '合併最終']);
}
公開 函式 合併步驟(&$上下文, $列ID, $字串, $分隔符號)
{
if (!isset($上下文)) {
$上下文 = [
'分隔符號' => $分隔符號,
'資料' => []
];
}
$上下文['資料'][] = $字串;
return $上下文;
}
公開 函式 合併最終(&$上下文)
{
return implode($上下文['分隔符號'], $上下文['資料']);
}
}
$SQLite = new 測試('/tmp/test.sqlite');
$SQLite->exec("create table `test` (`id` TEXT, `color` TEXT, `size` TEXT)");
$SQLite->exec("insert into `test` (`id`, `color`, `size`) values ('1', 'red', 'M')");
$SQLite->exec("insert into `test` (`id`, `color`, `size`) values ('1', 'green', 'M')");
$SQLite->exec("insert into `test` (`id`, `color`, `size`) values ('1', 'blue', 'S')");
$結果 = $SQLite->query("select `size`, groupConcat(`color`, ';') as `color` from `test` group by `size`");
while ($列 = $結果->fetchArray(SQLITE3_ASSOC)) {
print_r($列);
}