SET @order = 0;
SELECT * FROM
(SELECT @order := @order+1 AS `order`, ...)
UNION
(SELECT @order := @order+1 AS `order`, ...)
ORDER BY `order`
Use of ORDER BY for individual SELECT statements implies nothing about the order in which the rows appear in the final result because UNION by default produces an unordered set of rows.
ORDER BY e.`date` < NOW(), ABS(DATEDIFF(e.`date`, NOW()))
SELECT *
FROM calls
WHERE (SUBSTRING_INDEX(uniqueid, '.', 1), state) IN (
SELECT SUBSTRING_INDEX(uniqueid, '.', 1) AS uid, MAX(state)
FROM calls
WHERE src!=dst
GROUP BY uid
)
ORDER BY created
DESC LIMIT 0,15