【问题标题】:PHP ORM Mysql ordering using REGEX使用正则表达式的 PHP ORM Mysql 排序
【发布时间】:2011-04-30 00:49:43
【问题描述】:

你看我有一组按字母顺序排列的条目。虽然有些条目以“The”开头。我想要的是忽略“The”并从下一个单词开始排序。例如:

$titles->order_by("name", "ASC")->find_all() // Sample query
  • 亚伯拉罕
  • 全景
  • 恶毒的

我想要什么:

  • 亚伯拉罕
  • The Malevolent // 在排序中忽略“the”
  • 全景

我真正想要的”

  • 亚伯拉罕
  • Malevolent, The // 有点重新排列
  • 全景

我该怎么做?:

$titles->order_by("name", "ASC")->find_all();

如果不是,那么您有什么建议?

我有一种强烈的预感,即使用 REGEX 会达成交易。虽然我不知道如何不进入软件级别。

我使用的是 Kohana 3 ORM,上个月才开始使用。请对我放轻松。

非常感谢。

【问题讨论】:

    标签: php mysql regex


    【解决方案1】:

    您可以使用条件select 和substring 函数;这些函数的确切名称可能因您使用的方言而异。在 Transact-SQL 中:

    select if( left(name,3)='The', SUBSTRING(name,5), name) as name 
    from table 
    order by name
    

    还有另一种方式:使用条件查找“The”部分,并将其替换为空字符串:

    select TRIM(
      if(INSTR(shipnam,'The ') > 0, INSERT(shipnam,INSTR(shipnam,'Hyundai'),4,''),
      if(INSTR(shipnam,' The ') > 0, INSERT(shipnam,INSTR(shipnam,'Hyundai'),5,''),shipnam))
    ) 
    as shipname from tablename order by shipname
    

    根据你的列名和表名改变。

    【讨论】:

      猜你喜欢
      • 2012-02-26
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 1970-01-01
      相关资源
      最近更新 更多