mysql: optimize and finding best way for using repeated subqueries

Viewed 36

i use a query but i repeat the subquery 2 times.

is there a way that i can save in variable or optimize it so i dont call 2 times the subquery below:

SELECT phone FROM sku WHERE id=10

i tried this @phone:= (SELECT phone FROM sku WHERE id=10); but cant make it work

SELECT kred - 
   ( SELECT SUM((UNIX_TIMESTAMP()-timeStampSecondsColumn)* 0.1666666666666667) as total 
   FROM sku 
   WHERE phone=(SELECT phone FROM sku WHERE id=10) as Kred2
from users 
WHERE phone=(SELECT phone FROM sku WHERE id=10)
1 Answers

Can you try this, and let me know If It works for you?

SET @phone = (SELECT phone FROM sku WHERE id=10);
SELECT kred - 
   (SELECT SUM((UNIX_TIMESTAMP()-timeStampSecondsColumn)* 0.1666666666666667) as total 
   FROM sku 
   WHERE phone = @phone) as Kred2
from users 
WHERE phone=@phone;
Related