 |
mysql sort order แบบภาษามนุษย์ มีวิธี"จริงๆ"ไหมครับ? |
|
 |
|
|
 |
 |
|
แล้วเก็บข้อมูลเป็นอะไรหละครับ Int varchar tinyint
ถ้าเป็น varchar ก็ convert มันเป็น int ก่อนค่อยจัดเรียงก็ได้
ORDER BY CAST(`myid` AS SIGNED) DESC
|
 |
 |
 |
 |
Date :
2012-04-27 08:02:56 |
By :
deawx |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ข้อมูลเป็น varchar ครับ
ถ้าใช้ cast มันก็ไม่ได้อยู่ดี เพราะมันจะออกมาเป็น
cat1
cat2
cat3
cat19
cat04
cat5
ยิ่งกว่าเก่าอีกครับ
แล้วข้อมูลตรง field name นี้บางทีก็จะกรอกเป็น text ล้วนๆ บางทีก็ปนตัวเลข มั่วๆกันไปน่ะครับ 
|
 |
 |
 |
 |
Date :
2012-04-27 08:22:33 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้า format ไม่แน่นอนก็ยากที่จะเรียง
อาจจะ query ออกมาแล้วเขียน code เรียงอีกที่
โดยแยก ตัวเลขกับตัวอักษร ตัวเลขก็แปลงเป็น int
แล้ว sort ด้วยตัวอักษร แล้ว sort ด้วยตัวเลขอีกที
น่าจะใช้ได้นะ
** SQL มันพอทำได้รึป่าวไม่รู้ครับ
|
 |
 |
 |
 |
Date :
2012-04-27 08:28:09 |
By :
rootElement@kmutnb |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อย่างนี้ได้ไหมครับ
SELECT * FROM table ORDER BY CONVERT(name USING tis620) ASC
|
 |
 |
 |
 |
Date :
2012-04-27 09:11:53 |
By :
ไวยวิทย์ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้ามันยาก ผมว่าอย่าเก็บคำว่า cat เลยครับ
ถ้าอยากใส่ เอาไว้ตอน Query ออกมา แล้วค่อยมาวนลูปตัวเลขใส่คำว่า cat เอาจะง่ายกว่า
หรือว่า มันมีนัยยะอื่นที่จำเป็นต้องใ้ช้คำว่า cat ครับผม
|
 |
 |
 |
 |
Date :
2012-04-27 09:27:03 |
By :
deawx |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
CONVERT() ไม่ได้ช่วยอะไรน่ะครับ
คำว่า cat มันเป็นแค่ตัวอย่างครับคุณเดี่ยว คือข้อมูลตรงนี้จะคาดเดาไม่ได้เลยว่าจะกรอกอะไรลงไปบ้าง แต่ที่กรอกลงไปควรออกมาตามลำดับตัวอักษร ascending แบบภาษามนุษย์ครับ
ผมลองดูสคริปของชาวบ้านและของนอกแต่ละตัว ดูเหมือนมีปัญหานี้กันหมดเลย คือมันไม่สามารถจัดเรียงแบบมนุษย์ทำได้ ไม่เข้าใจทำไม mysql ละเลยปัญหานี้
พวก sql อื่นๆเป็นกันไหมครับ อย่าง mssql, access, ???
|
 |
 |
 |
 |
Date :
2012-04-27 10:31:45 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าส่งออกมาจากฐานข้อมูลทำเป็น array แล้วใช้ natsort() มันจะเรียงออกมาได้เป๊ะเลยนะ
แต่ว่า ถ้าข้อมูลเยอะมากๆ มันต้องแบ่งหน้า ซึ่งตอนนั้นมันจะใช้วิธีนี้ไม่ได้อีก ถึงเอามาใช้ได้โดยการยัดข้อมูลทั้งหมดลง array แล้ว natsort() แล้วแบ่งหน้าแบบ array มันก็เปลือง memory มีปัญหาเรื่องประสิทธิภาพเปล่าๆ
|
 |
 |
 |
 |
Date :
2012-04-27 10:36:32 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
http://stackoverflow.com/questions/153633/natural-sort-in-mysql
มีคนโพสต์โค้ดเอาไว้ตามนี้ ผมไม่รู้จะเอามาใช้ยังไง เลยไม่รู้มันจะเวิร์คมั้ย
author: scarsick
Code (VB.NET)
/**
* Returns a string formatted for natural sorting. This function is very useful when having to sort alpha-numeric strings.
*
* @author Alexandre Potvin Latreille (axp058)
* @param {nvarchar(4000)} string The formatted string.
* @param {int} numberLength The length each number should have (including padding). This should be the length of the longest number. Defaults to 10.
* @param {char(50)} sameOrderChars A list of characters that should have the same order. Ex: '.-/'. Defaults to empty string.
*
* @return {nvarchar(4000)} A string for natural sorting.
* Example of use:
*
* SELECT Name FROM TableA ORDER BY Name
* TableA (unordered) TableA (ordered)
* ------------ ------------
* ID Name ID Name
* 1. A1. 1. A1-1.
* 2. A1-1. 2. A1.
* 3. R1 --> 3. R1
* 4. R11 4. R11
* 5. R2 5. R2
*
*
* As we can see, humans would expect A1., A1-1., R1, R2, R11 but that's not how SQL is sorting it.
* We can use this function to fix this.
*
* SELECT Name FROM TableA ORDER BY dbo.udf_NaturalSortFormat(Name, default, '.-')
* TableA (unordered) TableA (ordered)
* ------------ ------------
* ID Name ID Name
* 1. A1. 1. A1.
* 2. A1-1. 2. A1-1.
* 3. R1 --> 3. R1
* 4. R11 4. R2
* 5. R2 5. R11
*/
CREATE FUNCTION dbo.udf_NaturalSortFormat(
@string nvarchar(4000),
@numberLength int = 10,
@sameOrderChars char(50) = ''
)
RETURNS varchar(4000)
AS
BEGIN
DECLARE @sortString varchar(4000),
@numStartIndex int,
@numEndIndex int,
@padLength int,
@totalPadLength int,
@i int,
@sameOrderCharsLen int;
SELECT
@totalPadLength = 0,
@string = RTRIM(LTRIM(@string)),
@sortString = @string,
@numStartIndex = PATINDEX('%[0-9]%', @string),
@numEndIndex = 0,
@i = 1,
@sameOrderCharsLen = LEN(@sameOrderChars);
-- Replace all char that has to have the same order by a space.
WHILE (@i <= @sameOrderCharsLen)
BEGIN
SET @sortString = REPLACE(@sortString, SUBSTRING(@sameOrderChars, @i, 1), ' ');
SET @i = @i + 1;
END
-- Pad numbers with zeros.
WHILE (@numStartIndex <> 0)
BEGIN
SET @numStartIndex = @numStartIndex + @numEndIndex;
SET @numEndIndex = @numStartIndex;
WHILE(PATINDEX('[0-9]', SUBSTRING(@string, @numEndIndex, 1)) = 1)
BEGIN
SET @numEndIndex = @numEndIndex + 1;
END
SET @numEndIndex = @numEndIndex - 1;
SET @padLength = @numberLength - (@numEndIndex + 1 - @numStartIndex);
IF @padLength < 0
BEGIN
SET @padLength = 0;
END
SET @sortString = STUFF(
@sortString,
@numStartIndex + @totalPadLength,
0,
REPLICATE('0', @padLength)
);
SET @totalPadLength = @totalPadLength + @padLength;
SET @numStartIndex = PATINDEX('%[0-9]%', RIGHT(@string, LEN(@string) - @numEndIndex));
END
RETURN @sortString;
END
GO
|
 |
 |
 |
 |
Date :
2012-04-27 11:48:23 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|