当前位置:网站首页>MySQL is sorted alphabetically

MySQL is sorted alphabetically

2022-07-06 06:22:00 Do not let it rot

Today's test raised a requirement , Sort by the first letter of the field

Because the general database code is utf-8 , His sorting method is to sort according to the English alphabet "a,b,c… "

 Insert picture description here
It can be seen from the above table that , As long as we convert the fields to be sorted into GBK Code and then sort , You can sort by field initials , How can I convert the code into GBK Well ? stay MySQL Functions are provided in CONVERT() , This function can be used to get the value of a type

 The usage of this function is  CONVERT( Field  USING GBK)

for example :

SELECT * FROM table ORDER BY CONVERT(field USING GBK) ASC

The following is my sql Example , My requirements are sorted by the initials of the units

Before no sorting

select
    ier.id,
    eor.id cid,
    eor.sort,
    ier.title,
    ier.summary,
    ier.expert_desc,
    ier.proposal,
    eor.status,
    ier.tenant_id,
    eor.level_one_code,
    eor.cycle_id,
    eor.monitor_organ_id,
    eor.project_id,
    so.name monitorOrganName 
from
    index_expert_reviews ier 
left join
    expert_organ_relation eor 
        on eor.expert_id=ier.id 
        and eor.tenant_id = ier.tenant_id 
left join
    sys_organ so 
        on so.id=eor.monitor_organ_id 
        and so.tenant_id = ier.tenant_id 
  LIMIT 0,
    10  

 Insert picture description here

After ordering

select
    ier.id,
    eor.id cid,
    eor.sort,
    ier.title,
    ier.summary,
    ier.expert_desc,
    ier.proposal,
    eor.status,
    ier.tenant_id,
    eor.level_one_code,
    eor.cycle_id,
    eor.monitor_organ_id,
    eor.project_id,
    so.name monitorOrganName 
from
    index_expert_reviews ier 
left join
    expert_organ_relation eor 
        on eor.expert_id=ier.id 
        and eor.tenant_id = ier.tenant_id 
left join
    sys_organ so 
        on so.id=eor.monitor_organ_id 
        and so.tenant_id = ier.tenant_id 
order by
    CONVERT( monitorOrganName  USING GBK) ASC  LIMIT 0,
    10  

 Insert picture description here
You can find that the virtual is at the bottom

原网站

版权声明
本文为[Do not let it rot]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060618557823.html