Mysql 按字符串排序,中间有空格

Mysql 按字符串排序,中间有空格,mysql,sql,database,Mysql,Sql,Database,我的表列“业务”如下所示: Michael Kors Baltimore Michael Kors Charlotte Michael Kors Michael Texas Kors Dallas Michael Kors Michael Kors Michael Kors Baltimore Michael Kors Charlotte Michael Kors Kors Dallas Michael Texas 我必须在字符串“Michael Kors”的这一列上应用order by,因

我的表列“业务”如下所示:

Michael Kors
Baltimore Michael Kors
Charlotte Michael Kors
Michael Texas
Kors Dallas
Michael Kors
Michael Kors
Michael Kors
Baltimore Michael Kors
Charlotte Michael Kors
Kors Dallas
Michael Texas
我必须在字符串“Michael Kors”的这一列上应用order by,因此排序结果应该如下所示:

Michael Kors
Baltimore Michael Kors
Charlotte Michael Kors
Michael Texas
Kors Dallas
Michael Kors
Michael Kors
Michael Kors
Baltimore Michael Kors
Charlotte Michael Kors
Kors Dallas
Michael Texas
若字符串包含子字符串Michael Kors,那个么它应该按字母顺序位于顶部。在上面的例子中,上面有两行完全匹配,然后巴尔的摩和夏洛特按字母顺序排在第三和第四。不担心其他不包含确切单词Michael Kors的字符串


我试过使用,但看起来它不适合使用带有空格的子字符串。感谢所有帮助。

您可以有多个级别的订购:

order by 
  locate('Michael Kors', business)=1 desc, 
  locate('Michael Kors', business)>0 desc, 
  business

第一个将精确匹配项排序到顶部,下一个级别对其余匹配行进行排序,第三个级别对所有其余行进行排序。

您可以有多个级别的排序:

order by 
  locate('Michael Kors', business)=1 desc, 
  locate('Michael Kors', business)>0 desc, 
  business

第一个将精确匹配项排序到顶部,下一个将对其余匹配行进行排序,第三个将对所有其余行进行排序。

您可以在
order by
子句中列出布尔表达式,并应用降序,以便将此表达式为true的记录排序在产生false的记录之前。然后在末尾指定字母顺序,以确定当所有其他表达式都不区分两条记录时的顺序:

select   * 
from     mytable
order by (business = 'Michael Kors') desc,
         (business like '%Michael Kors%') desc,
         (business like '%Kors%') desc,
         (business like '%Michael%') desc,
         business

您可以在
order by
子句中列出布尔表达式,并应用降序,以便将此表达式为true的记录排序在生成false的记录之前。然后在末尾指定字母顺序,以确定当所有其他表达式都不区分两条记录时的顺序:

select   * 
from     mytable
order by (business = 'Michael Kors') desc,
         (business like '%Michael Kors%') desc,
         (business like '%Kors%') desc,
         (business like '%Michael%') desc,
         business
试试这个

order by FIELD(business, 'Michael Kors’)
试试这个

order by FIELD(business, 'Michael Kors’)

一个字符串按哪个规则排序?@trincot更新了我的问题按哪个规则一个字符串按哪个规则排序?@trincot更新了我的问题谢谢@Nipun Sampath您的解决方案也起了作用,我正在评估所有建议中最快的一个,因为我必须在1000万数据上运行它。很高兴提供帮助:)谢谢@Nipun Sampath您的解决方案也起了作用,我正在评估所有建议中最快的一个,因为我必须在1000万数据上运行它。很高兴提供帮助:)