Answer the question
In order to leave comments, you need to log in
Sort data from textbox as number?
Hello!
There is a table with fields that contain mixed data (numbers with letters).
When getting data from this table, you need to sort them as numbers.
I used to work in conjunction with MySQL, but now I needed to switch to Postgress
used a query
select data from ".$prefix."$table WHERE pre_data = '$pre_data' ORDER BY (data+0)
// Получение списка столбцов
foreach ($out2 as $key => $row) {
$volume[$key] = $row['data'];
}
//Сортировка
array_multisort($volume,SORT_NUMERIC, SORT_ASC, $out2);
Answer the question
In order to leave comments, you need to log in
Extract all the numeric data you want to sort by from the data column into a new numeric type column and sort by it.
SELECT * FROM unnest(array['1','2б','2а','1а','2']) a ORDER BY a ASC;
For oracle, these tricks work - www.techonthenet.com/oracle/questions/sort1.php
For Postgre, I think it should work too.
ORDER BY substring(column_name, '^[0-9]+')::int
Or so that after the number the letters are sorted alphabetically
ORDER BY substring(column_name, '^[0-9]+')::int, substring(column_name, '[^0-9]*$')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question