Rails 中根据中文拼音对列进行排序的便携式方法

发布于 2024-10-16 13:08:48 字数 2679 浏览 2 评论 0原文

是否有一种可移植的方法可以使用 Activerecord 或标准 SQL 语句根据 Rails 中的汉语拼音对列进行排序,而不管底层数据库配置如何。如果这是不可能的,那么在 postgresql 上执行它的推荐方法是什么。

这里提供了一种mysql 数据库,CHARSET 为 gb2312。

    SQL code

        mysql> create table t_cosler( 
             -> f_PY char primary key, 
             -> cBegin SMALLINT UNSIGNED not null, 
             -> cEnd SMALLINT UNSIGNED not null -> ); 
        Query OK, 0 rows affected (0.09 sec) 

        mysql> insert into t_cosler values 
        -> ('A',0xB0A1,0xB0C4), 
        -> ('B',0xB0C5,0xB2C0), 
        -> ('C',0xB2C1,0xB4ED), 
        -> ('D',0xB4EE,0xB6E9), 
        -> ('E',0xB6EA,0xB7A1), 
        -> ('F',0xB7A2,0xB8C0),  
        -> ('G',0xB8C1,0xB9FD), 
        -> ('H',0xB9FE,0xBBF6), 
        -> ('J',0xBBF7,0xBFA5),  
        -> ('K',0xBFA6,0xC0AB), 
        -> ('L',0xC0AC,0xC2E7), 
        -> ('M',0xC2E8,0xC4C2), 
        -> ('N',0xC4C3,0xC5B5), 
        -> ('O',0xC5B6,0xC5BD), 
        -> ('P',0xC5BE,0xC6D9), 
        -> ('Q',0xC6DA,0xC8BA), 
        -> ('R',0xC8BB,0xC8F5), 
        -> ('S',0xC8F6,0xCBF9), 
        -> ('T',0xCBFA,0xCDD9), 
        -> ('W',0xCDDA,0xCEF3), 
        -> ('X',0xCEF4,0xD188), 
        -> ('Y',0xD1B9,0xD4D0), 
        -> ('Z',0xD4D1,0xD7F9);

         Query OK, 23 rows affected (0.16 sec) Records: 23 Duplicates: 0 Warnings: 0 
        mysql> select * from o_personnel;
        +------+------------+ 
        | A_Id | A_UserName | 
        +------+------------+ 
        | 1 | 首先 | 
        | 2 | 检查 | 
        | 3 | 我们 | 
        | 4 | 的二 | 
        | 5 | 进制 | 
        | 6 | 是否 | 
        | 7 | 适合 | 
        | 8 | 你的 | 
        | 9 | 平台 | 

        +------+------------+ 9 rows in set (0.00 sec) 
     mysql> select p.*,c.*
    -> from o_personnel p , t_cosler c
    -> where  CONV(HEX(left(A_UserName,1)),16,10) between c.cBegin and c.cEnd;

+------+------------+------+--------+-------+
| A_Id | A_UserName | f_PY | cBegin | cEnd  |
+------+------------+------+--------+-------+
|    4 | 的二       | D    |  46318 | 46825 |
|    2 | 检查       | J    |  48119 | 49061 |
|    5 | 进制       | J    |  48119 | 49061 |
|    8 | 你的       | N    |  50371 | 50613 |
|    9 | 平台       | P    |  50622 | 50905 |
|    1 | 首先       | S    |  51446 | 52217 |
|    6 | 是否       | S    |  51446 | 52217 |
|    7 | 适合       | S    |  51446 | 52217 |
|    3 | 我们       | W    |  52698 | 52979 |
+------+------------+------+--------+-------+
9 rows in set (0.00 sec)

mysql>

Is there a portable way to sort columns according to Chinese pinyin (汉语拼音) in rails using either Activerecord or standard SQL statement, regardless of the underlying database configuration. If this is not possible, what is recommend way to perform it on postgresql.

Here provide an approach for mysql database with CHARSET as gb2312.

    SQL code

        mysql> create table t_cosler( 
             -> f_PY char primary key, 
             -> cBegin SMALLINT UNSIGNED not null, 
             -> cEnd SMALLINT UNSIGNED not null -> ); 
        Query OK, 0 rows affected (0.09 sec) 

        mysql> insert into t_cosler values 
        -> ('A',0xB0A1,0xB0C4), 
        -> ('B',0xB0C5,0xB2C0), 
        -> ('C',0xB2C1,0xB4ED), 
        -> ('D',0xB4EE,0xB6E9), 
        -> ('E',0xB6EA,0xB7A1), 
        -> ('F',0xB7A2,0xB8C0),  
        -> ('G',0xB8C1,0xB9FD), 
        -> ('H',0xB9FE,0xBBF6), 
        -> ('J',0xBBF7,0xBFA5),  
        -> ('K',0xBFA6,0xC0AB), 
        -> ('L',0xC0AC,0xC2E7), 
        -> ('M',0xC2E8,0xC4C2), 
        -> ('N',0xC4C3,0xC5B5), 
        -> ('O',0xC5B6,0xC5BD), 
        -> ('P',0xC5BE,0xC6D9), 
        -> ('Q',0xC6DA,0xC8BA), 
        -> ('R',0xC8BB,0xC8F5), 
        -> ('S',0xC8F6,0xCBF9), 
        -> ('T',0xCBFA,0xCDD9), 
        -> ('W',0xCDDA,0xCEF3), 
        -> ('X',0xCEF4,0xD188), 
        -> ('Y',0xD1B9,0xD4D0), 
        -> ('Z',0xD4D1,0xD7F9);

         Query OK, 23 rows affected (0.16 sec) Records: 23 Duplicates: 0 Warnings: 0 
        mysql> select * from o_personnel;
        +------+------------+ 
        | A_Id | A_UserName | 
        +------+------------+ 
        | 1 | 首先 | 
        | 2 | 检查 | 
        | 3 | 我们 | 
        | 4 | 的二 | 
        | 5 | 进制 | 
        | 6 | 是否 | 
        | 7 | 适合 | 
        | 8 | 你的 | 
        | 9 | 平台 | 

        +------+------------+ 9 rows in set (0.00 sec) 
     mysql> select p.*,c.*
    -> from o_personnel p , t_cosler c
    -> where  CONV(HEX(left(A_UserName,1)),16,10) between c.cBegin and c.cEnd;

+------+------------+------+--------+-------+
| A_Id | A_UserName | f_PY | cBegin | cEnd  |
+------+------------+------+--------+-------+
|    4 | 的二       | D    |  46318 | 46825 |
|    2 | 检查       | J    |  48119 | 49061 |
|    5 | 进制       | J    |  48119 | 49061 |
|    8 | 你的       | N    |  50371 | 50613 |
|    9 | 平台       | P    |  50622 | 50905 |
|    1 | 首先       | S    |  51446 | 52217 |
|    6 | 是否       | S    |  51446 | 52217 |
|    7 | 适合       | S    |  51446 | 52217 |
|    3 | 我们       | W    |  52698 | 52979 |
+------+------------+------+--------+-------+
9 rows in set (0.00 sec)

mysql>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏剧牡丹亭 2024-10-23 13:08:48

我会大胆地说“不,无论使用 SQL 的数据库配置如何”。我不确定是什么控制 Rails 排序。

SQL 数据库管理系统中的排序顺序由排序规则控制。根据 dbms,您也许能够在服务器、数据库、表或列级别设置排序规则。而且,根据数据库管理系统的不同,您甚至可以指定在查询运行时使用的排序规则。

我非常确定 SQL Server 支持所有这些级别。 PostgreSQL 没有。我不知道MySQL。

要对拼音进行排序,请选择适当的排序规则。 (我不知道哪一个合适。)一些详细信息位于“区域设置支持”——不在 PostgreSQL 文档中的“排序规则”下。

我不确定 ruby​​ 在多大程度上依赖操作系统中的任何内容(区域设置、代码页、字符编码)来进行自己的排序。但如果它依赖于操作系统中的任何东西,我不得不说它可能不是 100% 可靠。 (不同操作系统下的不同实现可能会产生略有不同的结果。)

I'm going to go out on a limb and say "No, not regardless of the database configuration using SQL". I'm not sure what controls Rails sorting.

The sort order in SQL database management systems is controlled by a collation. Depending on the dbms, you might be able to set the collation at the server, database, table, or column level. And, depending on the dbms, you might even be able to specify a collation to be used at run time in a query.

I'm pretty sure that SQL Server supports all those levels. PostgreSQL does not. I don't know about MySQL.

To sort pinyin, choose an appropriate collation. (I don't know which one is appropriate.) Some of the details are under "Locale Support"--not under "collation"--in the PostgreSQL docs.

I'm not sure to what extent ruby relies on anything from the operating system--locale settings, code pages, character encoding--to do its own sorting. But if it relies on anything from the OS, I'd have to say it's probably not 100% reliable. (In that different implementations under different operating systems might have slightly different results.)

口干舌燥 2024-10-23 13:08:48

为了解决这个问题,我刚刚写了一个gem toPinyin,只需gem install toPinyin

require 'toPinyin'
words = "
检查
我们
的二
进制
是否
适合
你的
平台".split("\n")

words.sort! {|a ,b|   a.pinyin.join <=> b.pinyin.join }

To solve this problem, I have just written a gem toPinyin, simply gem install toPinyin

require 'toPinyin'
words = "
检查
我们
的二
进制
是否
适合
你的
平台".split("\n")

words.sort! {|a ,b|   a.pinyin.join <=> b.pinyin.join }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文