1

I have a field capacity in mytable .

I want to sort the fetch data by capacity from smallest to largest

these values are

6" x 12"
6" x 12"
6" x 18"
6" x 12"
10" x 20"
12" x 24"

I want this results

6" x 12"
6" x 12"
6" x 12"
6" x 18"
10" x 20"
12" x 24"

if I using this query

select * from mytable order by capacity ASC

than the result is

10" x 20"
12" x 24"
6" x 12"
6" x 12"
6" x 12"
6" x 18"

if I using this query

select * from mytable order by capacity + 0 ASC

than the result is

6" x 12"
6" x 12"
6" x 18"
6" x 12"
10" x 20"
12" x 24"
4

6 回答 6

4

对于表来说,这是一个非常糟糕的模式。最好的方法是创建一个表格,将它们()分成两列:宽度高度。例子,

CREATE TABLE Dimension
(
   ID INT AUTO_INCREMENT,
   `Width` INT,
   `Height` INT,
   CONSTRAINT tb_pk PRIMARY KEY (ID)
);

然后你现在可以像这样插入你的记录,

INSERT INTO Dimension (`Width`, `Height`) VALUES (6, 12), (6,18), (10, 12);

并简单地使用这个查询来订购结果

SELECT *
FROM Dimension
ORDER BY (`width` * `height`) DESC

但是要在不更改架构的情况下回答您的问题,USER DEFINE FUNCTION请在您的服务器中创建一个,

CREATE FUNCTION SPLIT_STR
(
  x VARCHAR(255),
  delim VARCHAR(12),
  pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
       LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
       delim, '');

然后使用这个查询,

SELECT CAST(SPLIT_STR(REPLACE(`capacity`, '"', '') ,'x', 1) as SIGNED) width,
       CAST(SPLIT_STR(REPLACE(`capacity`, '"', '') ,'x', 2) as SIGNED) height
FROM tableName
ORDER BY  (width * height) DESC

SQLFiddle 演示

于 2012-09-22T06:10:29.313 回答
3

你应该试试这个...

SELECT *, SUBSTRING_INDEX(capacity, 'x', 1) AS width, 
    SUBSTRING_INDEX(capacity, 'x', -1) AS height 
FROM mytable 
ORDER BY width+0 ASC, height+0 ASC
于 2012-09-22T06:02:44.357 回答
1

更改此查询

select * from mytable order by capacity ASC

select * from mytable order by trim(capacity) ASC

两天前它也帮助了我......

于 2012-09-22T05:57:16.653 回答
1

一种方法是循环遍历结果并将每个值相乘。之后使用自定义排序,您可以获得您正在寻找的结果。

$sortedArray = array();
$array       = array(
    '6" x 12"', '6" x 18"', '6" x 12"', '6" x 12"', '10" x 12"'
);

// loop through rowset
foreach ($array as $row) {
    $parts = explode(' x ', str_replace('"', '', $row));
    $sortedArray[][($parts[0] * $parts[1])] = $row;
}

// custom sort - PHP 5.3 required for anonymous functions
usort($sortedArray, function($a, $b) {
    return (array_keys($a) > array_keys($b));
});

或者,如果您没有 PHP 5.3,那么您可以像这样简单地更改 usort:

function mySort($a, $b)
{
    return (array_keys($a) > array_keys($b));
}

usort($sortedArray, 'mySort');
于 2012-09-22T05:50:28.753 回答
1

如果您无法重构数据库以将大小存储在两个数字列中,但您想在 mysql 中实现排序而不是 php 后处理,您可以尝试提取维度并将值转换为数字,例如

SELECT dimensions
FROM mytable
ORDER BY
    CAST(SUBSTRING_INDEX(dimensions, '"', 1) AS SIGNED) * 
    CAST(SUBSTRING_INDEX(LEFT(dimensions, LENGTH(dimensions)-1), ' ', -1) AS SIGNED)
    ASC

或者,更好的是,类似于 Ashwini 的回答:

SELECT dimensions
FROM mytable
ORDER BY
    (SUBSTRING_INDEX(dimensions, 'x', 1) + 0) * 
    (SUBSTRING_INDEX(dimensions, 'x', -1) + 0)
    ASC

您可能需要调整两个子字符串索引部分。

另一种提取所需值的方法:

  • 提取第一个维度(从开头到第一个“的子字符串:LEFT(dimensions, INSTR(dimensions, '"'))

  • 提取第二个维度(从 x 到末尾小于 1 的子字符串):RIGHT(LEFT(dimensions, LENGTH(dimensions-1)), INSTR(dimensions, 'x') + 1)

于 2012-09-22T06:10:03.163 回答
1

如果可以的话,为自己做点好事。为这些数据创建 2 个单独的列,如果您不想在 PHP 中连接这些值,请使用 SQL 联系:

select contact(width, '" x ', height, '"') as capacity from mytable order by width asc, height asc

此外,分别按宽度和高度排序更有效并产生所需的结果。

于 2012-09-22T06:19:10.957 回答