mysql自定义顺序通过混合数据类型 [英] mysql custom order by with mixed data types

查看:90
本文介绍了mysql自定义顺序通过混合数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的mysql查询中,我使用了一个自定义的by by语句,因此我可以按特定的顺序显示各种大小,而不是按字母顺序显示:

In the following mysql query I'm using a custom order by statement so I can display various sizes in a specific order instead of alphabetical:

select distinct size 
from product p left join productsizes ps 
             on p.productcode = ps.size_prodcode 
order by field(size, 'XXS', 'XS', 'S', 'M', 'L', 'XL', 'XXL', 'XXXL')

如果某些产品也具有数字尺寸,我该如何写顺序,以便将数字尺寸与自定义顺序一起按升序排列?

In cases where some products also have numeric sizes, how do I write the order by so that it places the numeric sizes in an ascending order along with the custom order?

所需输出的示例:

30、32、34,S,M,L

30, 32, 34, S, M, L

S,M,L,30、32、34

S, M, L, 30, 32, 34

推荐答案

找不到搜索字符串时,FIELD() 返回0.因此:

ORDER BY FIELD(size, 'XXS', 'XS', 'S', 'M', 'L', 'XL', 'XXL', 'XXXL'), size

这篇关于mysql自定义顺序通过混合数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆