开发者

how can I order like this in mysql [closed]

开发者 https://www.devze.com 2023-03-20 13:58 出处:网络
It's difficult to tell what is being asked here. T开发者_JS百科his question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. F
It's difficult to tell what is being asked here. T开发者_JS百科his question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

How can I order in mysql like

1 1/2 2 3/1 3/2 3/12 5 5/1 7 13/1

when i or der it bring 1 1/2 13/1 befor 5

format is x/x whre x is number


I think you need to split the column into two separate columns. I'm by no means a MySQL expert, you may have to wrap this inside a subselect or otherwise manipulate it, and as we've discussed, you'll need to convert these from strings into integers (I can't seem to make this work on the MySQL install I've got, but I never use it):

select
    SUBSTRING_INDEX(a,'/',1) as PartA,
    CASE WHEN LOCATE('/',a) > 0 THEN SUBSTRING_INDEX(a,'/',-1) ELSE '0' END as PartB
from
    <rest of query>

Where a is the name of this column. Like I say, I can't seem to fathom MySQLs syntax for converting these values into integers, and also, obviously, we then need to apply an ORDER BY clause.

Sorry this is an incomplete answer, but hopefully it will point you in the right direction, and no-one else seems to be answering...


Based on what I've just seen in your comment, I think we can get them numeric by:

select
    CAST(SUBSTRING_INDEX(a,'/',1) as decimal) as PartA,
    CAST(CASE WHEN LOCATE('/',a) > 0 THEN SUBSTRING_INDEX(a,'/',-1) ELSE '0' END as decimal) as PartB
from
    <rest of query>
0

精彩评论

暂无评论...
验证码 换一张
取 消