The SuperKat
The SuperKat

Reputation: 234

Mysql sort data from my table like described

My table looks like this, (let's just say it has no primary or any other key)

enter image description here

I want data to be in this format

enter image description here

help please :(

Upvotes: 2

Views: 68

Answers (2)

Chetan Singh Samant
Chetan Singh Samant

Reputation: 65

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

Upvotes: 1

M Khalid Junaid
M Khalid Junaid

Reputation: 64466

Using string functions you can nearly achieve that using following

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

Upvotes: 1

Related Questions