how to get highest integer value from mysql table PHP - php

how to get highest integer value from mysql table PHP
$getingstoredata=mysql_query('select number from number ');
while ($getstd=mysql_fetch_row($getingstoredata)){
print_r($getstd);
}
and table stricture is same like this image http://fastcoding.tk/table.png
i want to display highest integer value for example it 120 is highest value in this table

use MAX()
SELECT MAX(number) FROM number

You can simply use the Aggregate Functions.
SELECT MAX(field_name) FROM table_name GROUP BY field_name;
You can also use "order by" clause with DESC and fetch the first row from result-set.

select MAX(number) from number;

MAX() should serve your purpose

If your field is a mixed varchar where some values begin with letters, you can use this solution:
SELECT max(number) FROM table WHERE number REGEXP '^[0-9]+$';
the function max() will take the highest value
the regex sorts out every value that not begins with a number

$hinteger=mysql_query("SELECT MAX(integer) FROM number");

Related

How to find the nearest string value from table?

I have a table name "test" having a column code (string format) and data is like:
U298765
U298799
U210430
U210499
B239856
Now I want to get data by input field entry. If a user write U298750, I want show the nearest value U298765 and for U210401,U210430.
You can use the right function to fetch the number and then use following logic.
Select t.*
From test t
Order by abs(Right(code, length(code)-1) - Right(your_input, length(your_input)-1))
Limit 1
I am consodering that you need the nearest based on numbers only.
Try below query:
select code from test
order by abs(abs(substring(code,2,length(code)))-abs(substring('U298750',2,length('U298750'))))
Limit 1
In place of 'U298750' use your input
You seem to just want:
select t.*
from t
where code >= ? -- the input value
order by code desc
limit 1;
The ordering of strings alphabetically is sufficient for getting the "next" value after the string. There is no need to convert anything to numbers.

How to re-order a while loop by the sum of two of its values?

I have a table with names and numbers in which I make a SELECT and then a WHILE loop to display results, but the value I need to order the whole table, comes from the sum of three numeric fields on each row , and this is also a new value to display. I considered to convert this sum into an array and sort it, but how this sort can affect and reorder other data?
Do the arithmetic in the SELECT query then order it by that column.
SELECT field1+field2+field3 AS Sumof123 FROM Table1 ORDER BY Sumof123
Use MySql SUM()
SELECT (SUM(column1)+SUM(column2)+SUM(column3)) AS totalvalue FROM your_table ORDER BY 1 ASC

SQL SUM values from column and dividide by number of rows

I'm trying to calculate a percentual...and I need to sum all values from a column (Its already ok) but I need divide the result by number of rows...
select sum(voto) from tablename where id = numberHere
use COUNT to get the totalNumber of rows.
SELECT SUM(voto) / (COUNT(*) * 1.0)
FROM tablename
WHERE id = numberHere
by adding * 1.0 on the query will allow decimal places on the result.
or simply as
SELECT AVG(voto)
FROM tablename
WHERE id = numberHere
JW's answer is correct if you're looking specifically to do it by Summing/Dividing, but SQL has a function for that.
SELECT AVG(voto) FROM tablename WHERE id = numberHere
AFAIK, it automatically returns the same type as you input (except date columns, which should be parsed to seconds then re-encoded to date).
AVG should work, count(*) should work, you can also use ##rownum to get the number of rows returned by the statement if you need to do more with that number.

order by seriq asc show wrong results

Why when i use order by seriq asc and have numbers like "10000" "100000" "97000"
script show me the results:
1: 10000
2: 100000
3: 97000
?
because they are stored in your column as strings
1- try to change the column seriq from VARCHAR/CHAR to INT.
2- You can use CAST() to convert from string to int. e.g. SELECT CAST('123' AS SIGNED);
Your seriq column must be a numeric column for values to be sorted numerically. If it's a text column, values will be sorted in alphabetical order as per their collation. For example:
CREATE TABLE Test
(
Foo int
);
INSERT INTO Test VALUES (10000);
INSERT INTO Test VALUES (100000);
INSERT INTO Test VALUES (97000);
select * from Test order by Foo asc;
Fiddle
The quick fix is to have MySQL convert the string to a numeric value in the ORDER BY clause.
Change your query to:
ORDER BY seriq+0 ASC
Note that MySQL will attempt to convert any string to a numeric value, reading characters up to the first "invalid" character, e.g. '123A' will be converted to a numeric value 123.
Changing the column type to int is the best solution as it will give you the best data storage and performance. However, if that is not an option, the following workaround is possible:
select * from foo order by seqid+0
This forces a type cast of the order by column to int, and the sort happens numerically.

how to query get max id from varchar type and the values in numeric?

i have table and column id, and the values are 1, 2 ,3,...,10,11,12,13.
how to query get max id from varchar type ? i had try
select MAX(id) from table
but the result is 9, Please help ??
Looks like the values are strings and it selecting the maximum string. You have to cast them to numbers first if you want them to sort numerically. You can use CONVERT to do this:
SELECT MAX(CONVERT(id, SIGNED)) FROM table
You can also use CAST:
SELECT MAX(CAST(id AS SIGNED)) FROM table
They do nearly the same thing except CONVERT has some additional options if you need them.
You can cast the varchar to an integer - something like
SELECT MAX(CONVERT(id, SIGNED)) FROM table
http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html#function_convert
SELECT MAX(id+0) FROM table will do the trick

Categories