Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
create table tabl (
id serial, sort bigserial, pos_changed_at timestamp null
);
INSERT INTO tabl (sort)
VALUES
(
extract(epoch from now())*10000
),
(
extract(epoch from now() + interval '5 min')*10000
),
(
extract(epoch from now() + interval '10 min')*10000
);
SELECT
*
FROM
tabl;
<?php
$query = "SELECT sort FROM tabl ORDER BY sort;";
$stmt = $pdo->prepare($query);
$insert = "INSERT INTO tabl (sort) VALUES (:sort)";
$sth = $pdo->prepare($insert);
for ($i = 1; $i <= 20; $i++) {
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$sort = $rows[0]["sort"] + round(($rows[1]["sort"] - $rows[0]["sort"]) / 2);
$sth->execute(["sort" => $sort]);
}
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
//var_dump($rows);
while (key($rows) !== null) {
$a = current($rows)['sort'];
$b = next($rows)['sort'];
echo "$b - $a = " . $b - $a . PHP_EOL;
}