Sorting of double values from database - php

In the database I keep records to which codes are assigned (double format).
When calling records from the database I would like them to be sorted from the smallest to the largest one.
The problem arises when 1.2 > 1.10 which is understandable.
I would like it to be interpreted as follows: 1.10 > 1.2
I tried to do this by changing the type to TEXT.
Adding spaces at the end of the "code".
I have an idea to divide the numbers into segments and if the first segment is equal then sort by the second segment.
It seems logical, but I don't know how to translate it into code.
Could anyone help?

if your codes contain a valid decimal value the you could try converting the codes in order by as couple of integer
select codes
from my_table
order by ( CAST(left(codes, locate('.', codes)-1 ) AS UNSIGNED),
CAST(right(codes, length(codes -locate('.', codes)) AS UNSIGNED))

Related

SQL BETWEEN prices stored as string

I'm running into a problem here. I'm storing prices in my database as a string in the following format: 14.500,00 and 199,95. Sometime later I created this range slider so the users can filter on price as you can see in the provided image. For this to work, I needed to write a new query so I was thinking of a BETWEEN in SQL but this doesn't work on strings. Any ideas to filter on price with a range slider in SQL?
BETWEEN does work on strings. It works just fine -- with the strings ordered alphabetically.
Your problem is that BETWEEN on strings doesn't follow the numeric ordering. Well, that is normal. If I'm speaking French, I wouldn't expect an English speaker to understand me. The same with types. If I use BETWEEN on strings, then I expect the comparisons to be string-based, not numeric. (The same is true of dates, by the way.)
Fix your data so the values are stored as numeric/decimal values. These are numbers with a fixed number of decimal places, exactly what is needed for monetary values.
In most databases, you will need to get rid of the dollar sign. Something like this should work:
update t
set price = replace(price, '$', '');
alter table t alter column price numeric(10, 2); -- or whatever is appropriate
The exact syntax might vary, depending on the database.

How to display the column value descending when the column having spacial characters in mysql

How to display the column desc order when the column having spacial chars in mysql
I am using the follow query but not display correctly
SELECT quotation_pno FROM crm_quotation order by quotation_pno desc
My output coming like this
quotation_pno
PT/17/999
PT/17/1533
PT/17/1532
PT/16/1531
I want my output like this
quotation_pno
PT/17/1533
PT/17/1532
PT/17/999
PT/16/1531
Please help me
I'd argue, that the output is correct, but your assumptions are not. It looks to me, as if quotation_pno is some kind of textual column, right?
The sorting assumes, that you want to sort text and this works this way:
Set i to 0
Compare the i-th character of two strigns
If they are the same and the end is not reached, increase i by 1 and proceed with step 2
Otherwise order the two strings according to the value at the i-th position
(There are some things elided and the pseudocode is boiled down to the very basic, needed to understand the principle).
Applied to your example this means, when the comparison compares PT/17/999 and PT/17/1533 it looks at the characters 0 to 5 and "sees" that they are equal. When it compares the characters at position 6, they are '9' and '1'. Since the character '9' is considered to be greater than '1', PT/17/999 is placed before PT/17/1533.
How to solve the issue?
There are some ways coming into my mind, that will allow you to achieve the desired sort order.
First, you could prepend the numbers with zeros. This will allow you to re-use most of your existing structure, but will result either in very many zeros, or a system that is somehow limited, since you will be restricted to the number of digits you decided to use (or the sort will fail again).
The second possibility is, to store the parts in (additional) numerical columns in the table, e.g. one for year and one for the order number in this year. This is the more flexible approach, but involves more changes.

ORDER BY clause displaying numbers in output

I have a query that uses an ORDER BY clause in it.
This is potentially causing the output display numerical values, instead of the textual output as desired.
My question : What am I doing wrong? I'm trying to output the Ed_Name , GroupName fields from my query and it is outputting what appears to be like a single character from a database entry.
Any help is greatly appreciated. Thank you.
The data you are looking for resides in $paths['Ed_Name'] and $paths['GroupName'].
The values you see are the first characters of every column in your query.
33, 3, 5, changing occupation, Education, high school diploma or GED, 4
That happens because you loop through the columns and then try to output $job['GroupName'], where $job is in fact a string, hence it is first converting 'GroupName' to integer, which produces 0, and then gives you that first character.

how to sort out by desc mysql price with commas

$query = "SELECT * FROM mprice where price BETWEEN '1,000' AND '3,250' ORDER BY CAST(
REPLACE(price,',', '.') AS decimal(10,2)) LIMIT $from, $max_results"
This query is showing result like this
1,000
2,000
3,250
10,900
11,700
27,600
and I want like this
1,000
2,000
3,250
only
How can I do this? Please help me to fix this issue.
Thanks
This is somewhat bizarre functionality. I would recommend revisiting your schema design, if you have an option to do so, and utilizing a numeric type for your price column (you can include localization information in another column). That said, you can get it to work using the following query:
SELECT
*
FROM
mprice
WHERE
CAST(REPLACE(price, ',', '.') AS decimal(10,2)) BETWEEN 1.00 AND 3.25
ORDER BY
CAST(REPLACE(price, ',', '.') AS decimal(10,2))
Note that in your code you were attempting to restrict price to values between '1,000' and '3,250'. However, string comparison doesn't work well with numeric types, as it sorts lexicographically (alphabetically), treating numbers just like any other character. In this case, it was including values such as 10,900 and 27,600 because the first digit of these strings was between 1 and 3, which are the first characters of '1,000' and '3,250'.
When you cast the prices to numbers, you can correct this behavior. However, you also need to convert your filtering values to numbers, in this case 1.00 and 3.25.

Sort MySQL in Human

here's the code:
$sql_namesResult = mysql_query("SELECT name FROM `scrimaprovedlist` ORDER BY `scrimaprovedlist`.`eorank`");
eo rank is a NUMERICAL value for a rank (general, colonel, ect).
The problem is, when i set myself for 1, i am the top, but comes rank 10, instead of rank 2. how do i edit this to make it show in order:
1
2
3
10
20
30
I'm currently using "rank" instead of "eorank" because it is easier. but the problem is i have to manually edit the ranks over and over again so that they show in the correct order. Any ideas?
Viewable at http://www.thexcrew.com/modules.php?name=Roster
ORDER BY CAST(scrimaprovedlist.eorank AS INTEGER)
Your ranks are strings instead of integers so they will be sorted as a string unless you cast or convert them to integers which I've done above
figured out a way, i changed my Numerical value to an alphabetical value. using only 17 ranks, i am able to substitute #'s for letters. thanks for the help anyway :)

Categories