SQLite3 データベースの中で、シーケンスを並べる
strnatcmp 関数を登録します。
<?php
$db = new SQLite3(":memory:");
$db->exec("CREATE TABLE test (col1 string)");
$db->exec("INSERT INTO test VALUES ('a1')");
$db->exec("INSERT INTO test VALUES ('a10')");
$db->exec("INSERT INTO test VALUES ('a2')");
$db->createCollation('NATURAL_CMP', 'strnatcmp');
$defaultSort = $db->query("SELECT col1 FROM test ORDER BY col1");
$naturalSort = $db->query("SELECT col1 FROM test ORDER BY col1 COLLATE NATURAL_CMP");
echo "default:\n";
while ($row = $defaultSort->fetchArray()){
echo $row['col1'], "\n";
}
echo "\nnatural:\n";
while ($row = $naturalSort->fetchArray()){
echo $row['col1'], "\n";
}
$db->close();
?>
default:
a1
a10
a2
natural:
a1
a2
a10