Mysql如何实现这个功能,可以用存储过程。
====================
table表中有个字段tag,字段的内容是由一个个被“|”分隔的子字符串组成的字符串,同一个分类的这些记录的“|”个数是相同的。现在希望按一定的条件从table表选出按tag某个子字符串排序的记录。
如:
类别字段 tag字段
A类      asd|325|pfyh|dfgfd|上海
A类      asds|353|hdgt|dds|河北
A类      dgdg|3532|sdfs|dsfds|辽宁
A类      dsgds|43646|ddsg|dfgs|广东现在要按tag字段第n个子符串进行排序:
select * from table order by ***;(***为字段tag的第n个子字符串)。
请问如何实现?

解决方案 »

  1.   

    参考:http://blog.chinaunix.net/u/29134/showart_1002486.html
      

  2.   

    一条sql:(想要以第n个字符串排序,实际书写时,把下面的n替换成实际的数字,n 从零开始)select * from table where .... order by SUBSTRING_INDEX(SUBSTRING_INDEX(tag字段,'|',n),'|',-1)实现是实现了,不过,效率上,不太高。
      

  3.   

    select * ,SUBSTRING_INDEX(SUBSTRING_INDEX(tag字段,'|',n),'|',-1)  as myorder from table where .... order by myorder desc
    等,都可以。
      

  4.   

    http://blog.chinaunix.net/u/29134/showart_1002486.html 我看了,但是没法留言,抱歉。
    你的思想很好。