开发者

mysql sorting bits

I have a Field "stars" in my table.

1 means **
2 means ***
4 means ****

now they can be combined like 3 means 2.5 and 6 means 3.5 stars. It an only be 2 values next to each other so something like 5 cant occur. This works well with the output. The Problem now is the sorting. Right now i just sort by the field stars. Obviously this does not work correctly because the correct order would be like:

2.5stars, 3stars, 3.5stars

but those mean:

3,2,6

overall the correct sorting would be like:

1,3,2,6,4,12,8,24,16

I have a pagebrowser with limit and filters an everything so i dont want to select all rows a开发者_JAVA百科nd sort by hand. Is there a way to get this kind of sorting directly in my query?

Edit:

To be clear, this comes from Bitwise..

00001 = **
00010 = ***
00011 == 2.5*
00100 = ****


It's not pretty, but you can give this a shot.

SELECT x FROM y WHERE z
ORDER BY
CASE stars
    WHEN 1  THEN 1
    WHEN 3  THEN 2
    WHEN 2  THEN 3
    WHEN 6  THEN 4
    WHEN 4  THEN 5
    WHEN 12 THEN 6
    WHEN 8  THEN 7
    WHEN 24 THEN 8
    WHEN 16 THEN 9
    ELSE 10
END CASE
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜