Anda harus meningkatkan ini, tetapi pada dasarnya beginilah cara Anda melakukannya. Anda dapat menggunakan self-join
SELECT uid, MAX(myvalue)-MIN(myvalue) AS diff FROM
(SELECT a.* FROM tablename AS a
LEFT JOIN tablename AS a2
ON a.uid=a2.uid AND a.myvalue<=a2.myvalue
GROUP BY uid,myvalue
HAVING COUNT(*)<=2
) a
GROUP BY uid