0

varchar 型の mysql クエリの列を並べ替えようとしていますが、数値を持つ場合と持たない場合があります。

for example it may have the values

2012-10
2012-41
2012-1

which should be sorted as follows:
2012-1
2012-10
2012-41

but if the values are :
M-1
M-13
M-5

it should be sorted as :
M-1
M-5
M-13

and if null values are present it should be last.

それが可能かどうかはわかりません。助けてください

4

1 に答える 1

1

ハイフンの後の数値で数値を並べ替える場合は、次のようにします。

ORDER BY column IS NOT NULL, SUBSTRING_INDEX(column, '-', 1), CAST(SUBSTRING_INDEX(column, '-', 2) AS DECIMAL)

column IS NOT NULLnull 列を最後にCAST()ソートし、式は残りを数値でソートします。

于 2013-08-06T07:53:47.657 に答える