How to get row of the table with maximum value - php

I have a table as follow :
id date custid billno month amount balance
64 07-Jun-2015 1 102 5 9192 0
134 05-Jul-2015 1 172 6 9744 0
235 01-Aug-2015 1 277 7 4032 0
435 04-Sep-2015 1 461 8 3024 0
747 22-Sep-2015 1 597 9 2875 0
958 06-Nov-2015 1 789 10 3100 0
I want to get the row which has month max i.e. I want to get the row with month 10
If I use the following query then it gives the row with month 9
$get_lst = mysql_query("select * from `ledger` where `custid`='$custid' order by `month` DESC") or die(mysql_error());
$get_lst = mysql_fetch_assoc($get_lst);
print_r($get_lst);
Please help me what I am missing in the query ?

It looks like you are storing your values with the wrong type. The Date column should be type of DATE and your month column should be type of INT or even better TINYINT.
Storing month as VARCHAR causes the db to sort it as string. Ordering strings will start checking char by char (comparing '9' with '10' will result in '9' > '1').
After changing your column to either INT or TINYINT your query will work.
To increase the performance for larger tables, you could also use "LIMIT 1" in your query, in that way you won't fetch the whole table, if you only need one row.

Related

PHP sql: select a chunks of rows

I am trying to select chunks of 5 rows every time I make a database call.
In my DB, some columns might have been deleted and others not, so the ID column can have "gaps", like missing numbers, something like this:
ID name category ....
33 xxxx xxxxxxx
34 xxxx xxxxxxx
38 xxxx xxxxxxx
40 xxxx xxxxxxx
41 xxxx xxxxxxx
45 xxxx xxxxxxx
49 xxxx xxxxxxx
...
I have ben trying with something like this:
$query = "SELECT * FROM Products LIMIT 33, 5";
I understood that adding LIMITto the query it will take the next following 5 rows starting by 33, however it does not seem to be working.
Any better suggestion to get 5 rows starting by a specific ID each time??
If you read manual for limit clause, you will see definintion:
LIMIT {[offset,] row_count ...
So in your case LIMIT 33, 5 means SKIP 33 records and TAKE 5 following records, and not FIND RECORD WITH ID = 33 and TAKE 5 following records.
So, your option is to use a where-clause:
SELECT * FROM Products WHERE ID > 33 ORDER BY ID ASC LIMIT 5
The values that follow the LIMIT keyword are the offset and the number of rows from the complete recordset to return. They are not IDs or other values returned by the query.
Read about MySQL SELECT statement.
If you want to get 5 rows starting with the one having ID 33 then your query is:
SELECT *
FROM Products
WHERE ID >= 33
ORDER BY ID
LIMIT 5
Without LIMIT 5, the query above returns all the rows having ID greater than or equal with 33, sorted in ascending order by their ID. The LIMIT 5 clause (which is a short for LIMIT 0, 5) tells MySQL to return only the first 5 rows from the list.

Mysql query to get closest record

I have a table
id startmileage endmileage price
1 1 2 30
2 2 3 50
3 3 4 70
4 4 5 100
5 5 25 4.5
6 25 35 7
In this table i have stored data to get price between mileages (from start to end mile)
I am able to get the price between 1 difference value like 1 to 2, 2 to 3. But for the values between 5 to 25 and 25 to 35 i have tried a query which will work for both the closest and exact value like this
SELECT *
FROM table
ORDER BY ABS( startmileage - myValue )
LIMIT 1
But this query only works for one input (startmileage), As i need to use both start and end mileage to get the best closest record.
Can someone please tell me the best query for this ?
ORDER BY
(
greatest(startmileage - $myvalue ,0)
+
greatest($myvalue - endmileage ,0)
) ASC , startmileage DESC
if the $myvalue is within boundaries, this will be equivalent to zero. If it is before startmileage, first part will rose while second is zero. If $myvalue is after endmileage, first part will be zero and second will rose up.
function greatest( ??? , 0 ) will return ??? only when it is greater then zero, returning zero otherwise.
edit: A bit optimized query (works the same way):
ORDER BY
greatest(startmileage - $myvalue , $myvalue - endmileage ) ASC
, startmileage DESC

PHP/MySQL total by member

Like this a result :
75 Ansari 5 10
88 Koodoo 4 0
90 Koodoo 14 0
83 Koodoo 5 0
82 Koodoo, 6 0
81 Koodoo 4 0
79 Koodoo 5 0
74 Savage 1 0
80 Strike 2 36
87 Strike 4 15
78 Sullivan 3 15
77 Sullivan 2 0
I would like to get the total for each member for the last 2 columns (Hours and Minutes).
My query look like that :
SELECT
*
FROM
$tbl_name
ORDER BY
player
If someone would have a quick fix for that I would appreciate it.
Probably you want something like this (but you didn't provide a schema so I'm guessing on the field names).
SELECT `id`, `player`, SUM(`hours`) as hours, SUM(`minutes`) as minutes FROM `$tbl_name` GROUP_BY `player`;
You need to GROUP BY the user's name and SUM the totals for the hours and minutes columns.
A Simple example of this is:
SELECT `id`,
`user`,
SUM(`hours`) as tot_hours,
SUM(`mins`) as tot_mins
FROM `test`
GROUP BY `user`;
You can see this example run at this SQLFiddle
Note: With that fiddle, I removed the extra comma in this line (I assumed it was a typo)
82 Koodoo, 6 0
If that comma is supposed to be there, you just need to adjust that one insert statement to add the comma within the quotes of the name. That will adjust your query outcome as Koodoo, won't group with the other Koodoo values.

Select max win series: sql query

MySQL
I have table, where i store user_matches and it result:
n_match id_user id_score
1 55 1
1 66 0
This mean, 'user with id=55 win match with id=1 to user with id=66'.
So, we have 10, 100, 1000 matches, where user win or lose to opponents:
n_match id_user id_score
1 55 1 (win)
1 66 0
2 55 0 (lose)
2 77 1
3 55 1 (win)
3 77 0
4 55 1 (win)
4 77 0
5 55 1 (win)
5 77 0
Ok. As u can see, user win 3 matches without losing (win series)- and that's what i need from my query.
Question: How could i get from this table the longest series of won matches? Is it possible without looping on sql side or server side- just from query?
Thx.
Edit: One of solution i just now understand,- to get all matches as string like 001010101111010101011, then split it into array of strings with separator '0' -> [1, 1, 1, 1111, ...] and just take the longest string length.
But in this case i have to write server side code =\ That's not good, but mb the fastest.
The best way to do this is to calculate the cumulative number of losses for any match. For a sequence of wins, this value is constant. You can then use group by to get the length of the longest such sequence.
This version of the query is database-neutral. It uses subqueries to get the counts:
select user_id, max(NumWinsInRow)
from (select user_id, cumlosses, count(*)-1 as NumWinsInRow
from (select m.*,
(select sum(case when id_score = 0 then 1 else 0 end) from user_matches m2 where m2.id_user = m.id_user and m2.n_match <= m.n_match
) as CumLosses
from user_matches m
) t
group by cumlosses, user_id
) t
group by user_id
This query should run faster if you have an index on user_matches(id_user, n_math, id_score).

How to define which number from DATABASE is near (round) to getting number?

I have an SQL TABLE like below. So on PHP page I enter the temp 44 or 43 and the number 1 or 2. For example I have entered the number: 2 and the temp :44...Php calcullates from formula (how it calcullates I think write about it is not important) and gets the number like 46.5777. So this number is not on DATABASE. I have to define which number from DATABASE is near (round) to 46.5777? How can I define it and show it?
num temp decimal
1 43 44.5760
2 44 47.0827
2 43 45.9396
3 44 48.5177
3 43 47.3053
Find closest numeric value in database - same problem.
In your example query will be SELECT * FROM table WHERE num = 2 AND temp = 44 ORDER BY ABS(decimal - 46.5777) LIMIT 1;. It should return the nearest value of 46.5777.

Categories