(PHP 8 >= 8.4.0)
Pdo\Sqlite::createCollation — 注册一个用户定义函数,用作 SQL 语句中的排序函数
此方法类似于Pdo\Sqlite::createFunction(),但它注册用于对字符串进行排序的函数。
示例 #1 Pdo\Sqlite::createCollation() 示例
<?php
$db = new Pdo\Sqlite('sqlite::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->sqliteCreateCollation('NATURAL_CMP', 'strnatcmp');
foreach ($db->query("SELECT col1 FROM test ORDER BY col1") as $row) {
echo $row['col1'] . "\n";
}
echo "\n";
foreach ($db->query("SELECT col1 FROM test ORDER BY col1 COLLATE NATURAL_CMP") as $row) {
echo $row['col1'] . "\n";
}
?>
以上示例将输出
a1 a10 a2 a1 a2 a10