来自我的表的Mysql排序数据,如

时间:2017-12-13 09:49:53

标签: mysql sql sorting

我的表看起来像这样,(我们只是说它没有主键或任何其他键)

enter image description here

我希望数据采用这种格式

enter image description here

请帮助:(

2 个答案:

答案 0 :(得分:1)

使用字符串函数几乎可以使用以下

来实现
select a.*
from demo a
join (select value,substring_index(name,'_',2) company_name 
      from demo 
      where substring_index(name,'_',-1) = 'name'
) b on substring_index(a.name,'_',2) = b.company_name
order by b.value asc,substring_index(a.name,'_',2) asc,substring_index(name,'_',-1) desc 

Demo

答案 1 :(得分:1)

SELECT a.* FROM css_01 a LEFT JOIN (SELECT substring_index(name, '_', 2) as meta_like_key, value FROM css_01 wpm1 WHERE name like "company_%_name" ) b ON substring_index(a.name, '_', 2) = b.meta_like_key ORDER BY b.value ASC ,substring_index(a.name,'_',2) asc,a.name desc