i have a table "orders" in mysql database , which contains column "order_number" . And because each order has several rows ( depending on size of the eshop order ) , the column "order_number" looks like this :
1
1
1
2
2
2
2
2
3
3
3
.... etc ... currently there is about 1000 orders , which means several thousands rows in table . What i am trying to do is just retrieve highest order number ... sounds easy enough :
$result=mysql_query("SELECT max(order_number) as max FROM orders");
$lastordernumber = mysql_fetch_array($result);
echo $lastordernumber["max"];
...and this returns "99" , while currently highest order number is close to 1000 . any ideas ?
thanks !
This occurs when the "order_number" is a string. You should store it as a numeric value, if that is what you want it to be. However, you can treat it as numeric for the purposes of this query:
select max(order_number + 0) as max
from orders;
Alternatively, you could write the query as:
select order_number
from orders
order by len(order_number) desc, order_number desc
limit 1;
Related
I want to select the lowest value in the 'n' sql column and increment it by 1. If the lowest value is present in multible rows, then I want to choose among those rows at random. For instance, in the example table below where the lowest number is 0 I want to randomly choose between the rows where ID = 1, 2, or 3.
ID
n
1
0
2
0
3
0
4
1
5
2
The code below will increment all three rows where n = 0. How do I randomly select just 1? I use Adminer as database.
$sql = "UPDATE studycondition SET n=n +1 WHERE n=(SELECT MIN(n) FROM studycondition)";
use
"UPDATE studycondition SET n=n +1 WHERE n=(SELECT MIN(n) FROM studycondition) limit 1"
Add limit in your inner query and update by id
UPDATE studycondition SET n = n + 1
WHERE id=(SELECT id FROM studycondition order by n asc limit 1)
I have the following table in a database:
I have a variable $slotsNeeded which is the number of consecutive slotID with isFree = true needed from the table. How may I output all of the possible combinations using echo, up to a maximum of 5?
For example:
When $slotsNeeded = 1 output should be echo 1."<br>".3."<br>".4."<br>".5."<br>".7; // Maximum 5 combinations
When $slotsNeeded = 2 output should be echo 3, 4."<br>".4, 5."<br>".7, 8;
When $slotsNeeded = 3 output should be echo 3, 4, 5;
I have tried everything but can't seem to find a solution. Please advise. Thank you.
Edit: my actual table was created like this:
CREATE TABLE $tbl_Appts (
slotID INTEGER(255) PRIMARY KEY,
time TIME,
patientID BIGINT,
bedID INTEGER(255),
isFree BOOLEAN DEFAULT 1,
)
And currently looks like this:
In SQL, you can find the first of a series of available slots using lead(). To get five in a row:
select t.*
from (select t.*,
lead(slotId, 4) over (partition by isFree order by slotId) as slotId_4
from $tbl_Appts t
where isFree = 'True'
) t
where slotId_4 = slotId + 4;
The lead() looks at the row 4 rows ahead where isFree is true. If that row is the current slot plus 4, then all the intermediate rows are true and you have 5 in a row.
I have the query below, which is supposed to get all the "record" fields from a mysql table called users. The record field's values must be bigger than 0 for it to count. and the query only returns true if 3 or more records found (where record > 0)
The query below makes sense to me, but its returning the following PHP error : Operand should contain 2 column(s)
$query = "
SELECT * FROM users u
WHERE (
SELECT COUNT(record) AS record,
SUM(CASE WHEN record > 0 THEN 1 ELSE 0 END)
FROM users
) >= 3
";
Can SUM and COUNT not be used in the same query? I've used them simultaneously in the past with no problems.
Any help would be great thank
EDIT ---------------------
Table : users
--------------
id value
--------------
1 1
2 1
3 1
4 0
5 0
6 -1
7 -10
8 0
I'd like to only return a result if the value field in the table above is bigger than 0. But I also only want to return a result if the total number of values found in the table (where value > 0) are 3 or more.
You can just use the count function to count, a where to limit the data, and the having function to check that you have the number of records you want.
select count(*) as counted
from users
where record > 0
having counted > 3
Demo: http://sqlfiddle.com/#!9/29ed41e/1
With the above query your PHP will have the results as the first index, or counted depending on how you fetch. You don't need to loop the fetch because there will only be 1 row returned.
Roughly:
$row = $query->fetch();
return $row['counted'];
The number of rows will be 1 so you don't want to count the number of rows, you want the actual returned value.
So for example I have a table users, with a column 'count' and a column 'uid' which is the primary key.
uid | count
1 | 20
2 | 20
3 | 20
4 | 20
4 | 18
I want to select exactly one row which has count less than or equal to the present row. For example, I have the row where uid = 2.
Now I want to select a column which has count less than or equal to the present count value which is "20". and I want to select exactly one row which is closest to it.
Now I will have the choice to select either the row which has uid = 3 or uid = 4. In such case, I will want to select the column with the lowest uid value such that it is greater than the present uid value which is 2. Therefore I will want uid = 3 as my result.
How to put this in a mysql query ?
So something like this?
SELECT * FROM users
WHERE count <= 20
ORDER BY count DESC, uid ASC
LIMIT 1
That'll sort the results so that everything above 20 is discarded, and you get the rest in decreasing count order, with lower user ids taking priority if there are multiples of the same count. The LIMIT 1 restricts the query to return only one row.
If you want to make the comparison to an existing row, your easiest bet is to do this:
SELECT * FROM users
WHERE count <= 20
AND uid != 2
ORDER BY count DESC, uid ASC
LIMIT 1
SELECT * FROM users
WHERE count <= 20
AND uid IN (3,4)
ORDER BY uid ASC,count DESC
LIMIT 1
position | Average | gpmp
1 70.60 2.0
2 60.20 2.3
3 59.80 4.8
4 59.80 4.8
5 45.70 5.6
Hie All,
As above table, I need to arrange the position according to the lowest gpmp and the highest average. But when the both average and gmp are the same, I will need to have the position to be the same.
For example, position 3 and 4 have the same average and gpmp. How do I generate the mysql query or using php function so that after they detect the same average and gpmp and change the position 4 to 3.
Which mean after the function is generated it will become like the table below.
position | Average | gpmp
1 70.60 2.0
2 60.20 2.3
3 59.80 4.8
3 59.80 4.8
5 45.70 5.6
Here's a simple way to update the table as you described in your post - taking the sequential positions and updating them accordingly. It doesn't calculate the positions or anything, just uses the data already there:
UPDATE `table` t SET position = (
SELECT MIN(position) FROM (SELECT * FROM `table`) t2 WHERE t.Average = t2.Average AND t.gpmp = t2.gpmp
)
I'd give something like the following a try, through it does assume a primary key is on this table. Without a primary key you're going to have issues updating specific rows easily / you'll have a lot of duplicates.
So for this example I'll assume the table is as follows
someTable (
pkID (Primary Key),
position,
Average,
gpm
)
So the following INSERT would do the job I expect
INSERT INTO someTable (
pkID,
position
)
SELECT
someTable.pkID,
calcTable.position
FROM someTable
INNER JOIN (
SELECT
MIN(c.position) AS position,
c.Average,
c.gpm
FROM (
// Calculate the position for each Average/gpm combination
SELECT
#p = #p + 1 AS position,
someTable.Average,
someTable.gpm
FROM (
SELECT #p:=0
) v,someTable
ORDER BY
someTable.Average DESC,
someTable.gpmp ASC
) c
// Now regroup to get 1 position for each combination (the lowest position)
GROUP BY c.Average,c.gpm
) AS calcTable
// And then join this calculated table back onto the original
ON (calcTable.Average,calcTable.gpm) = (someTable.Average,someTable.gpm)
// And rely on the PK IDs clashing to allow update
ON DUPLICATE KEY UPDATE position = VALUES(position)
(pseudo code)
select * from table
get output into php var
foreach (php row of data)
is row equal to previous row?
yes - don't increment row counter, increment duplicate counter
no - increment row counter with # of duplicates and reset duplicate counter
save current row as 'previous row'
next
you can try something like this in php:
$d= mysql_query('select distinct gpmp from tablename order by gpmp');
pos= 1;
while($r= mysql_fetch_array($d)){
mysql_query('update tablename set position='.$pos.' where gpmp='.$r['gpmp']);
$pos++;
}
You only need to "expand" the idea to take averange in account too.