Want to get lowest value from sql - php

In my database there are 5 values: 50, 75, 95, 125 and 200. When I query the minimum value I get 125, but I should get 50.
Here is my code:
$result_upTICKET = mysql_query("SELECT ID, EID, COMMISSION, MIN(PRICE) as PRICE FROM `tickets` WHERE EID='$EID_UPcoMing' AND STATUS='1'");
while($row_upTICKET = mysql_fetch_array($result_upTICKET))
{
$PRICE_upTICKET= $row_upTICKET['PRICE'];
$COMMISSION_upTICKET= $row_upTICKET['COMMISSION'];
}
What did I get wrong?

Try this one
SELECT ID, EID, COMMISSION, PRICE FROM `tickets` WHERE EID='$EID_UPcoMing' AND STATUS='1' ORDER BY PRICE ASC LIMIT 1

you are using where condition also. It filters rows first then apply/Select columns and min. I am sure that your where condition pull out row with price 50 and that's why min is coming as 125 instead of 50

Please execute your query like,We need to convert into interger
mysql_query("SELECT ID, EID, COMMISSION, MIN(CONVERT(PRICE, SIGNED INTEGER)) as PRICE FROM tickets WHERE EID='$EID_UPcoMing' AND STATUS='1'");

Related

Insert row with cell amount dependent of previous row cell amount

I have a system balance table "A", where the column "amount" is a debit/credit column, like this:
id amount balance
1 100 100
2 -30 70
3 40 110
4 -20 90
If a new deposit is made of i.e. $50, the new row would have to be inserted as:
5 50 140
I could achieve this by selecting last balance:
SELECT id, balance
FROM A
ORDER BY id DESC LIMIT 1
and then inserting new row:
INSERT INTO A (amount, balance)
VALUES (50, previous_balance+50)
But, is there a way to achieve this with only one query? Would it be efficient on large databases? Thank you!
The correct way to do it is :
INSERT INTO A(amount,balance)
VALUES (50, 50 +
(SELECT B.balance
FROM(SELECT balance FROM A ORDER BY id DESC LIMIT 1) AS B)
);
Because mysql can't modify the same table which you use in the SELECT part
Just combine your 2 queries:
INSERT INTO A (amount, balance)
VALUES (50, (
SELECT balance
FROM (SELECT * from A)
ORDER BY id DESC LIMIT 1
)+50)
You can use this query:
INSERT INTO A (amount, balance)
SELECT "50", SUM(balance)
FROM ( SELECT id+1 as id, 50 balance FROM A
UNION ALL
SELECT id, balance FROM A
ORDER BY id DESC LIMIT 2
) t

mysql order by column within first column

is it possible to order within the first column like
select * FROM table where........ order by price desc and distance, distance
price holds values
0, 10, 24, 30 ...
and distance are values out of the lon/lat calculation. and now it displays it like this:
value1(price=30) ... 19,8km
value2(price=24) ... 8,2km
value3(price=10) ... 48km
//and then it starts with the entries in which price is 0 with the correct distance order
value4(price=0) ... 1,20km
value5(price=0) ... 1,28km
value6(price=0) ... 2,74km
and so on...
and what i want is, that within the ones where price is > 0 it shall first display them BUT with the distance as order method, then the ones with price = 0 and distance
so basically it should ignore the price value itself, just price > 0 before 0 and order by distance within the price. how could that query look like? thanks for any help :)
so this is what i want it to be;
value2(price=24) ... 8,2km
value1(price=30) ... 19,8km
value3(price=10) ... 48km
value4(price=0) ... 1,20km
value5(price=0) ... 1,28km
value6(price=0) ... 2,74km
and so on...
using order by
example
select * from price order by asc
or
select * from price order by desc
up to you
How about creating a calculated column to sort on:
SELECT *, IF(price > 0, 0, 1) AS pricegtzero FROM table WHERE........ ORDER BY pricegtzero, distance

Selecting a record where data in anouther column is at min

I need to get the record of two numbers in two columns. But I need to find where the min value is of one of the columns. And find the number that is aligned to that min number.
Right now I have the following:
$sql = "SELECT ID, MIN(price) AS minPrice FROM my_table";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo $row["minPrice"]; // This works
echo $row["ID"]; // This is not the number that is in the record where minPrice is.
If you are looking for one row, the simplest method is order by and limit:
select t.*
from t
order by t.col2 asc
limit 1;
You need to use subqueries.
Select ID from my_table t, (select min(price) as minPrice from my_table) where t.price =minPrice;
Didn't tested it, but it should work.
It returns all the ids with the minimal price.

MySQL Group By + Order

I've got a problem with grouping my rows.
Example Table
ID, GroupID, INFO, COUNTER
1, 123456, INFO, 21
2, 654321, INFO, 20
3, 123456, INFO, 30
4, 654321, INFO, 11
First of all, I'd like to display them with this:
SELECT *
FROM table
GROUP BY GroupID
ORDER BY COUNTER DESC
LIMIT 0, 60
So it should display only one of group type. It does, but the order is not good. So I think it's not getting the right number of counter paired to the GroupID.
The right displayed result would be: (the main order selector has to be the count)
ID, GroupID, INFO, COUNTER
3, 123456, INFO, 30
2, 654321, INFO, 20
How should I solve?
SELECT tablename.* FROM tablename
WHERE tablename.COUNTER =
(SELECT MAX(COUNTER) FROM tablename AS f WHERE f.GroupID = tablename.GroupID)
ORDER BY tablename.COUNTER DESC
Edited : This will get you the complete rows containing the max value of COUNTER for each GroupID, and order the final results by COUNTER desc.

MySQL MAX subquery

I have a problem with MySQL at this moment.
I'm trying to determine the highest float value from my table like this:
SELECT `id`
FROM `LOTRESULTS`
WHERE
`value`= (SELECT MAX(value) FROM `LOTRESULTS`) AND
`lot_id` = 180
ORDER BY `id` DESC
LIMIT 1
This works whenever I choose let's say id 180, but none of the other combinations work.
There are 3 entries with 180 as lot_id, and 2 occurences with 179 as lot_id, etc.
It just works randomly, it fails on most of the entries in the DB.
Am I doing something wrong? Should I change the datatype of the value column?
Thanks in advance guys!
p.s. I've also tried:
SELECT `id` FROM `LOTRESULTS` WHERE `value`= (SELECT MAX(value) FROM
`LOTRESULTS`) AND `lot_id` = 180
and
SELECT `id` FROM `LOTRESULTS` WHERE `value`= (SELECT MAX(value) AS
`value` FROM `LOTRESULTS`) AND `lot_id` = 180
with the same results...
Your inner query is going to fetch the highest value field ANYWHERE in the table, regardless of which lot it's part of. The outer query then tries to retrieve all the ids that have that max value AND are part of a specific lot.
If the max value belongs to lot 123, but you're fetching the ids for lot 456, you'll get no results.
The query should be:
SELECT id
FROM LOTRESULTS
WHERE value = (
SELECT MAX(value) AS value
FROM LOTRESULTS
WHERE lot_id = 180
) AND lot_id = 180
The doubled lot_id = 180 handles the case where two+ different lots may share the same maximum value. Without the 'outer and', you'd get the ids for the two+ lots.
You don't really have to use sub-query, or JOIN,
a simple where + order + limit will do :-
SELECT id FROM LOTRESULTS WHERE lot_id = 180 ORDER BY value DESC LIMIT 1;
SELECT *
FROM LOTRESULTS
WHERE whatever...
ORDER BY value DESC
LIMIT 1

Categories