I have an SQL query for a PHP API that includes calculations for fields like current_device, is_online, and expire_in. Should I handle these calculations in the PHP backend, or is it acceptable to perform them within the SQL query? Which is better?
Here is my query:
SELECT
ud.id as device_id,
ud.user_device_serial,
ud.user_device_name,
ud.user_device_platform,
ud.user_device_model,
ud.user_ip_address,
ul.id as login_id,
ul.token_expire,
CASE
WHEN ul.user_device_serial = '5506b5bdbe18be149bdcaf16a2665b73' THEN 1
ELSE 0
END as current_device,
CASE
WHEN ul.token_expire > UNIX_TIMESTAMP() THEN 1
ELSE 0
END as is_online,
CASE
WHEN ul.token_expire > UNIX_TIMESTAMP() THEN
CONCAT(
FLOOR((ul.token_expire - UNIX_TIMESTAMP()) / 3600), ' hr ',
FLOOR(((ul.token_expire - UNIX_TIMESTAMP()) % 3600) / 60), ' min'
)
ELSE '0 hr 0 min'
END as expire_in
FROM
user_device ud
LEFT JOIN
user_login ul
ON
ud.user_id = ul.user_id AND ud.user_device_serial = ul.user_device_serial
WHERE
ud.user_id = 278913 AND
ud.status = '1' AND
ud.approved = '1'
ORDER BY
ud.id;