Mysql query to get closest record - php

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

Related

Cumulative average mysql php

I have a table name meal
id name carbs protein fat
1 one 10 30 18
2 one 17 4 2
3 one 27 6 7
Now I want to get Cumulative Average of carbs
my query is
SELECT (100*(sum(m.carbs)/(sum(m.carbs)+sum(m.fat)+sum(m.protein))))AS Percantage_carbs FROM `meal` AS m
My query gives result 52.2580645
But I t will be
52.8848076
You Query will result in 44.628099173554 which is absolutely correct.
Total: 10+30+18+17+4+2+27+6+7 = 121
Carbs Only: 10+17+27 = 54
(54 / 121) * 100 = 44,63%
SQL Fiddle: http://sqlfiddle.com/#!2/3d6e5/3
The result of the data given and the query you're using is: 44,628099174
I guess your query should be something like this (your missing the group by):
SELECT (
100 *
(sum(m.carbs)
/
(sum(m.carbs)+ sum(m.fat)+ sum(m.protein)))
) AS Percantage_carbs
FROM `meal` AS m
GROUP BY name

Order By SQL Server PHP

I would like order the result of my select ids starting with 16, 15 and 17.
How could I do this?
My Select:
SELECT id_t_produtos
FROM table
ORDER BY nullif(id_t_produtos, 16) ASC
The expected response:
- 16
- 15
- 17
- 1
- 2
- 3 ...
If I understand well, I would do something like this
select ids from table
order by
case ids when 16 then 0
when 15 then 1
when 17 then 2
else 3
end,
ids
replace ids by id_t_produtos if that's the ordering field, not really clear...
SELECT ids
FROM table
ORDER BY CASE id_t_produtos
WHEN 16 THEN 1
WHEN 15 THEN 2
WHEN 17 THEN 3
WHEN 1 THEN 4
WHEN 2 THEN 5
WHEN 3 THEN 6
ELSE 7 END ASC, id_t_produtos ASC

Select max difference between 2 rows with the same value (Statistics)

I have a table with some values inserted on a daily basis, something like this
-----------
id | count
-----------
1 | 97
2 | **97**
3 | 59
4 | 62
5 | 47
6 | 59
7 | 59
8 | **97**
-----------
I need to get the max day difference between repeated values, i.e, as you can see the 1st and 2nd value is 97, that is 1 day difference, but the next occurrence of 97 is 6 days later, so I need to get this "Max" difference (6).
same thing for 59, the max day difference is 3 days (3) -- between day 3 and 6.
At this moment I`m using php arrays like this Example:
$q = " SELECT id FROM table WHERE VALUE = 97 ";
// etc ... the array looks like this
$array = {1, 2, 8};
then I get the "Max" difference, but I just want to know if there is any way to do this in mysql, thanks
EDIT:
//if we list only the column "count":
44 5 *97* 74 5 **97** 7 3 2 31 9 8 4 2 1 **97** 4 7 7 8 *97*
step1 : "97" is in 3rd position, then in 6th position (diff = 3)
step2 : "97" is in 6th position, then in 16th position (diff = 10)
step3 : "97" is in 16th position, then in 21st position (diff = 5)
step4 : MAX diff = 10
I must complain about this, I posted this question at 08:59 AM, I reloaded the page 1 minute later (At 9:00) and it was already down-voted, there was no time to read and understand the question, this is absurd
Both work:
http://sqlfiddle.com/#!2/243f2/22
select a.blahday ad, max(b.blahday) bd, a.blahday - max(b.blahday) diff from blah a join blah b using (blahcount)
where blahcount = 97
and a.blahday > b.blahday
group by ad
order by diff desc
limit 1
http://sqlfiddle.com/#!2/243f2/25
select a.blahday ab,
(select max(blahday) from blah where blahcount = a.blahcount and blahday < a.blahday) bd
from blah a
where blahcount = 97
order by ab - bd desc
limit 1
Try this:
select
(`a`.`max_id` - `b`.`min_id`) as `max_day_diff`, `a`.`count`
from
(select max(id) as `max_id`, `count`
from `table` group by `count`
) a
inner join
(select min(id) as `min_id`, `count`
from `table` group by `count`
) b
on `a`.`count` = `b`.`count`
This is the fiddle:
http://sqlfiddle.com/#!2/1a6a3/10
This will give max_day_diff as 0 for rows with only one count value (like 62, 47 in your case)
I think this will get you what you're looking for.
SQLFiddle!
That's a pretty funky table you've got there, btw.

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 can I select all the values of a database and choose one of them by a percent

What I want to do is to select a value of the database,
Lets say:
id ---- giftid ---- userid
1 1 481
2 1 422
3 7 123
4 9 542
5 1 122
6 1 455
For example, there are 4 users that want to have the same giftid:
1, 2, 5, 6
It means that each one will have 25% to be chosen.
How can I make the "percent selection"?
Assuming every userid can only claim a giftid once, you can use the ORDER BY RAND() in MySQL. This will firstly select all the rows from table table where the giftid is 1 and then the results are ordered randomly. The LIMIT 1 ensures that only the first record is returned
SELECT * FROM table
WHERE giftid = `1`
ORDER BY RAND()
LIMIT 1
Are you looking this?
SELECT giftid, 1.0 / COUNT(*) percentSelection
FROM tableName
GROUP BY giftid

Categories