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.
Related
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.
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 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.
Here's my MySQL Table
When I carry out the following query : SELECT MAX( Animal_ID ) FROM info_table
I get "9"
Shouldn't it be 10 ?
How can I resolve the error ?
Thanks!
My guess is that animal_id is being stored as a character field rather than as a numeric field. If you want the numeric max, try this:
select max(animal_id + 0)
from info_table
When you add two values together in MySQL and one is a character string, then the initial numeric characters (if any) are converted to a number.
Judging by the sample data in your other columns, you have made all your columns of type VarChar, i.e. text. The "anomalous result" you are seeing is one of the many reasons this is a bad idea.
Sorting a series of strings will generally be done in "alphabetical", or "lexical" order - "b" comes before "z", but not before "aaa". Strings consisting entirely of digits may look like numbers, but they will sort like any other string - so "1" comes before "2", but so does any string beginning with "1", such as "1a", "10", "100". This is generally not what you want.
Rather than working around this and the other problems you will encounter with, e.g. date fields, always set your tables up with correct types on every column.
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 :)