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.
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.
I have a table with a column where I have data stored this way:
1:29,3:20,5:0,4:0,2:76
I want to make a query request in PHP (or MySQL in general) and get rows where there is a number less than 10 after the " : ".
Therefore, in this case I have ( 29 , 20 , 0 , 0 , 76 ), and because I have a number less than 10, I will want to take this row.
In general, storing comma separated values is not considered a good design in SQL, as it leads to violation of data normalization rules and in addition faces many other issues. But since you have already done it and want a regex solution to your problem, you can use this regex,
:[0-9](,|$)
Here is a demo
In mysql you can write a query like this,
select * from tablename where columnname regexp ':[0-9](,|$)';
This will give you rows where at least one comma separated value is less than ten following a colon.
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.
I have a large mysql database which is about 10gb large. One of the tables in the database is called
clients
In that table there is a colum named
case
The date this client is created is mixed into the number within this column.
Here is an example of an entry in case
011706-0001
The 06 part means this client was created in 2006. I need to pull all the clients that were created in 2015 and 2016. So I need to query for anything that case has a 15 or 16 before the dash.
For example, 000015-0000 or 000016-0000
Is there a way to do this with only mysql? My thought process was I would have to query the whole column then use php to preg_match()
I am worried that based on the size of the database this would cause problems.
To locate rows that have a case column value that contains '06-' (the characters 0 and 6 followed by a dash ...
One option is to use a LIKE comparison operator:
SELECT ...
FROM clients t
WHERE t.case LIKE '%06-%'
ORDER BY ...
The percent sign characters are wildcards in the LIKE comparison, which match any number of characters (zero, one or more.)
MySQL will need to evaluate that condition for every row in the table. MySQL can't make use of an index range scan operation with that.
SELECT ...
FROM clients t
WHERE t.case LIKE '%15-%'
OR t.case LIKE '%16-%'
ORDER BY ...
That will evaluate to true for any values that include the sequence of three characters '15-' or '16-'.
If there's a more standard format for the values in the case column, where the value always starts with exactly six characters representing date 'mmddyy-nnnnn' and you only want to match the 5th thru 7th characters, you could use the underscore wildcard character which matches any one character (in the LIKE comparison) for example... using four underscores
t.case LIKE '____16-%'
Or you could use a SUBSTR function to extract the three characters from the case value, and perform an equality comparison...
SUBSTR(t.case,5,3) = '15-'
SUBSTR(t.case,5,3) IN ('15-','16-')
It's also possible to make use of a REGEXP comparison in place of the LIKE comparison.
In terms of performance, all of the above approaches are going to need to crank through every row in the table, to evaluate the comparison condition.
If that date value was stored as a separate column, as a DATE datatype, and there was an index with that as the leading column, then MySQL could make effective use of a range scan operation, for a query like this...
WHERE t.casedate >= '2015-01-01'
AND t.casedate < '2017-01-01'
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.