Sort MySQL in Human - php

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 :)

Related

Sorting of double values from database

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))

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.

MySQL sort within one column of data

For example, if I had a column in a database called RANDOM that has random bits of information distinguished by their end notation like this:
RANDOM
1. 12312 KM, 201 M, 1213 H, 101029 DOLLARS
2. 231 KM, 2351 M, 754 H, 345 DOLLARS, 120 L, 1201 FT
3. 2324 M
Some entries have other miscellaneous but important data points while others my only have one or two.
I would like to sort using only data within column RANDOM.
$RESULT = mysqli_query($CON, "SELECT * FROM TABLE WHERE RANDOM CONTAINS 'M' ORDER BY 'NUMBER BEFORE M'");
Therefore this would find the 3 rows that contain 'M' and then sort by the number in front of 'M'. Similarly with other variables like KM or DOLLARS. Is this possible using pure MySQL in a single statement?
Your sort would require a 2 aspirin headache with
ORDER BY substr('str',locate(str,a,b),locate(str,b,c))
If I understand the question then yes you could do it in one line , just requires some manipulation as your data is all in the one column.
SELECT * FROM
(SELECT *, CAST(LEFT(RANDOM, INSTR(RANDOM, '.') - 1) AS UNSIGNED) AS ORD_NUMBER
FROM `test_table`
) DERIVED_TABLE ORDER BY ORD_NUMBER DESC
Basically we are getting the number at the start by using INSTR to find the first . ( assuming this is the format for all items) and then to be safe we cast that as an integer. The data is returned using a derived table so then we sort on our dynamically calculated ord_number that's the result from filtering. Hope that helps.
So using this principle you can filter out however you like :)
But maybe rethink the db Design as doing anykind of query like this means maybe your RDBMS isn't setup correct (simple solution new column with that value :))

Finding the nearest numeric match in a database to what a user has inputted in php

I need to find a way in which to find a field in a database which; numerically, is the closet match to what a user has inputted in a web form and submitted.
Deeper explanation:
In a database table there are two rows with a field entry of 80.1 and 80.7. If a site user enters 80 into an input element on a web form and submits that value, the handling script looks up that table and finds a row with 80.1 and chooses this as the closet match.
If it was the other way round, i.e. the user entering a floated integer (80.6), the table looking for the nearest rounded number i.e. (90) that would be easy. I'm obviously missing something and thought there would be an in built function in PHP for this.
Apologies if I have not explained myself well, if so please let me know.
SELECT ABS($user_value - numeric_field) as nearest, ...
FROM yourtable
ORDER BY nearest ASC
LIMIT 1
basically, take the difference of the number field you want and the user-provided value, then sort by the difference ascending and then return the first row.
So an exact match would have a difference of 0 and come out first. And then pick out whichever one is "closest" if there's no exact match.
try this
SELECT * FROM `table_test1` where `price` > 80 order by `price` asc limit 1
SELECT ABS(price - 80) AS nearest FROM table ORDER BY nearest ASC LIMT 1;
Difficult to give you an exact answer, however here is some pseudo SQL which might help you out:
SELECT number, abs((number * 100) - ({$search_number} * 100)) AS distance
FROM your_table
ORDER BY distance
LIMIT 1

How to get the available area codes?

This is a pretty complex MySQL query for me.
what i have is a table of phone numbers in the format of a US phone number 8153216458.
Example:
TABLE: numbers(number)
4512163215
4512158211
4512110579
8812163215
9405462136
3021548641
What i want is to list the available area codes ( as i'm selling numbers ) without repeating them, Some query that is based on the first 3 digits & finally ordered correctly.
Output:
302
451
881
940
Any solution? i don't mind if it's still using php manipulation.
Thanks
Try this:
select distinct substring(number, 1, 3)
from numbers;
Or, as mentioned by Jeff in the comments, left is also an option:
select distinct left(number, 3)
from numbers;
Check out documentation for string functions on MySQL.
something like this?
select distinct substring(number, 0, 3) as 'number'
from numbers
order by number

Categories